数据连接池配置 全局 、局部配置之分
下面就我玩的全局的配置记录下来(本人用的是oracle):
1. 先将准备工作做好 将oracle的 class12.jar放到 tomcat下的comm/lib中;
2. 接着在tomcat的 conf/service.xml文件的 <GlobalNamingResources>域内
添加配置代码:
<Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:oracle" username="oracle" password="oracle" maxActive="50" maxIdle="20" maxWait="10000" />
3.在tomcat的 conf/context.xml文件中加入代码:
<ResourceLink name="jdbc/oracle" global="jdbc/oracle" type="javax.sql.DataSourcer"/>
若是没在 tomcat的 conf/context.xml文件中加入上边的代码 则会报 Cannot create JDBC driver of class '' for connect URL 'null' 这是因为你的服务器的全局JNDI资源,而用InitialContext去找server的resource当然找不到了,要想找到server的resource就得在web application中的context环境里加入一个指向该全局resource的ResourceLink。
4.接着在你的项目的 web.xml文件中添加:
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/oracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
5.配置文件都完成接下来工作就是测试,看配置的jndi 是否正确,下面是测试用的到的java代码:
Context ctx = new InitialContext(); ctx = new InitialContext(); //DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/oracle"); //查找JNDI数据源名 Context envContext = (Context)ctx.lookup("java:/comp/env"); DataSource ds = (DataSource)envContext.lookup("jdbc/oracle"); conn = ds.getConnection();
注解:
global -->The name of the linked global resource in the global JNDI context.
name -->The name of the resource link to be created, relative to the java:comp/env context.?
type -->The fully qualified Java class name expected by the web application when it performs a lookup for this resource link.