设为首页 收藏本站
查看: 405|回复: 0

[经验分享] Resin和Tomcat的JNDI数据连接池配置

[复制链接]

尚未签到

发表于 2017-2-5 11:19:38 | 显示全部楼层 |阅读模式
  
先说Resin的JNDI数据池连接配置(Resin版本3.1)
有两个地方可以共你设置
1, Resin 下的conf/resin.conf 里有这一部分 ,很明显告诉你在这设置:
<!--
- Sample database pool configuration
-
- The JDBC name is java:comp/env/jdbc/test
<database>
<jndi-name>jdbc/mysql</jndi-name>
<driver type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/test</url>
<user></user>
<password></password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
-->
这个地方我想是设置全局JNDI的部分
2,在你的工程文件如webapps/YourJspApp/WEB-INF/web.xml里 的</web-app>前写一下部分,(说白了就是把1,中的部分写到web.xml里就可以了,方便把说明我统一用的UTF-8编码)
<database>
<jndi-name>jdbc/YourDataBase</jndi-name>
<driver type="com.mysql.jdbc.Driver">
<url>jdbc:mysql://YourDataBaseServerIP:PORT/YourDataBase?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF8</url>
<user>USERNAME</user>
<password>USERPASSWORD</password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
就这样,配置部分就完工了.
Tomcat的配置,最让人头痛.(我使用的版本Tomcat6.0.13)
首先,大家要注意,Tomcat的不同版本Tomcat的JNDI的配置是不同的,像Tomcat5.5以前的版本和Tomcat5.5以后的版本,如Tomcat6.0配置很不一样,Tomcat5.0或以前的配置网上说的很多了,我也收录了几篇明白点的文章.(在小站文章搜索里填关键词"池"就有Tomcat其他版本配置)现在把Tomcat6.0的配置说一下:
首先,你不用配置conf/下的web.xml也不要改server.xml,因为5.5以上的版本不需要修改这两个全局配置文件,否则,您自己可以试试.关键是修改conf/context.xml工程上下文配置文件和你自己的工程文件里的WEB-INF/web.xml工程配置文件.修改如下:
Conf/context.xml
在<context></context>之间写JNDI资源配置信息
<Resource name="jdbc/YourDataBase" type="javax.sql.DataSource" auth="Container"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
maxWait="5000"
maxActive="4"
password="USERPASSWORD"
username="USERNAME" url="jdbc:mysql://YourServerIP:PORT/YourDataBase?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF8"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
/>
保存
你的工程文件下WEB-INF/web.xml里的</web-app>前
<resource-ref>
<description>MyApp DataBase Connection</description>
<res-ref-name>jdbc/YourDataBase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
保存OK
重起TOMCAT运行通过!可想而知TOMCAT6做了很大的改进不仅仅在内存优化上还是配置上比以前都有进步.
JNDI JAVABEAN:
package com.jndi;
import java.sql.Connection;
import java.sql.*;
import javax.sql.*;
import javax.sql.DataSource;
import javax.naming.*;
import javax.naming.NamingException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
public final class JndiDB
{
public static synchronized Connection getJndiConnection() throws Exception
{
try{
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");//似乎java:/comp/env也可以大家不妨一试
DataSource ds = (DataSource)envCtx.lookup("jdbc/YourDataBase");
// DataSource ds = (DataSource)intCtx.lookup("java:comp/env/jdbc/YourDataBase");
return ds.getConnection();
}catch(Exception e){
throw e;
}
}//END getJndiConnection();
}//End Class
官方参考:
Tomcat6.0
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
部分官方资料如下:
Context configuration
Configure the JNDI DataSource in Tomcat by adding a declaration for your resource to your Context.
For example:

<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<!-- username and password: MySQL dB username and password for dB connections -->
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->

<!-- url: The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
</Context>
3. web.xml configuration
Now create a WEB-INF/web.xml for this test application.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
4. Test code
Now create a simple test.jsp page for use later.

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>
<html>
<head>
<title>DB Test</title>
</head>
<body>
<h2>Results</h2>

<c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar ${row.bar}<br/>
</c:forEach>
</body>
</html>
That JSP page makes use of JSTL‘s SQL and Core taglibs. You can get it from Sun‘s Java Web Services Developer Pack or Jakarta Taglib Standard 1.1 project - just make sure you get a 1.1.x release. Once you have JSTL, copy jstl.jar and standard.jar to your web app‘s WEB-INF/lib directory.
Finally deploy your web app into $CATALINA_HOME/webapps either as a warfile called DBTest.war or into a sub-directory called DBTest
Once deployed, point a browser at http://localhost:8080/DBTest/test.jsp to view the fruits of your hard work.
Oracle 8i, 9i & 10g
0. Introduction
Oracle requires minimal changes from the MySQL configuration except for the usual gotchas :-)
Drivers for older Oracle versions may be distributed as *.zip files rather than *.jar files. Tomcat will only use *.jar files installed in $CATALINA_HOME/lib. Therefore classes111.zip or classes12.zip will need to be renamed with a .jar extension. Since jarfiles are zipfiles, there is no need to unzip and jar these files - a simple rename will suffice.
For Oracle 9i onwards you should use oracle.jdbc.OracleDriver rather than oracle.jdbc.driver.OracleDriver as Oracle have stated that oracle.jdbc.driver.OracleDriver is deprecated and support for this driver class will be discontinued in the next major release.
1. Context configuration
In a similar manner to the mysql config above, you will need to define your Datasource in your Context. Here we define a Datasource called myoracle using the thin driver to connect as user scott, password tiger to the sid called mysid. (Note: with the thin driver this sid is not the same as the tnsname). The schema used will be the default schema for the user scott.
Use of the OCI driver should simply involve a changing thin to oci in the URL string.

<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
username="scott" password="tiger" maxActive="20" maxIdle="10"
maxWait="-1"/>
2. web.xml configuration
You should ensure that you respect the element ordering defined by the DTD when you create you applications web.xml file.

<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3. Code example
You can use the same example application as above (asuming you create the required DB instance, tables etc.) replacing the Datasource code with something like

Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
//etc.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-337748-1-1.html 上篇帖子: 连接池c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool对比测试 下篇帖子: 使用tomcat把应用程序注册成Windows服务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表