`
YOUNG918
  • 浏览: 185204 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Resin和Tomcat的JNDI数据连接池配置

阅读更多


先说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.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics