omcat 6的配置和以前的不同,不推荐在server.xml中进行配置,而是在context.xml中进行配置才是更好的方法。是站点目录下的 context.xml文件,不是tomcat_home/conf下的。tomcat_home/webapps/yourApp/META-INF /context.xml,没有的话就创建一个,这样可以在不同的网站下单独配置连接池了,并且不需要重启Tomcat,Tomcat会自动重载。
context.xml:
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< Context reloadable ="true" crossContext ="true" >
<!-- Default set of monitored resources -->
< WatchedResource > WEB-INF/web.xml </ WatchedResource >
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
< Resource
name ="ds/test" //其中ds/test是数据源名称,通过JNDI调用
auth ="spark"
type ="javax.sql.DataSource"
driverClassName ="oracle.jdbc.driver.OracleDriver"
url ="jdbc:oracle:thin:@localhost:1521:orcl"
username ="scott"
password ="test"
maxActive ="20"
maxIdle ="10"
maxWait ="-1" />
</ Context >
< Context reloadable ="true" crossContext ="true" >
<!-- Default set of monitored resources -->
< WatchedResource > WEB-INF/web.xml </ WatchedResource >
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
< Resource
name ="ds/test" //其中ds/test是数据源名称,通过JNDI调用
auth ="spark"
type ="javax.sql.DataSource"
driverClassName ="oracle.jdbc.driver.OracleDriver"
url ="jdbc:oracle:thin:@localhost:1521:orcl"
username ="scott"
password ="test"
maxActive ="20"
maxIdle ="10"
maxWait ="-1" />
</ Context >
最后配置成功后在java代码中获得Connection对象
Context context =
new InitialContext();
//获得数据源
DataSource ds = context.lookup( "java:comp/env/ds/test" );
//获取连接
Connection conn = ds.getConnection();
//获得数据源
DataSource ds = context.lookup( "java:comp/env/ds/test" );
//获取连接
Connection conn = ds.getConnection();