Resin的JNDI数据池连接配置
有两个地方可以共你设置
有两个地方可以共你设置
1, Resin 下的conf/resin.xml 里添加<database>节点
<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&useUnicode=true&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的配置
tomcat的不同版本Tomcat的JNDI的配置是不同的,像tomcat5.5以前的版本和tomcat5.5以后的版本.如tomcat6.0配置很不一样,tomcat5.0或以前的配置网上很多,现在把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://ServerIP:PORT/DataBase?autoReconnect=true&useUnicode=true&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>
配置完成后, 编写测试代码:
try{
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env"); //官方的example写法
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;
}