连接池的配置

来源:互联网 发布:南昌seo外包 编辑:程序博客网 时间:2024/06/05 12:47
* 1.context.xml
1.配置tomcat安装目录conf/context.xml文件<Context><Resource name="jdbc/message" auth="Container" //name指的是指定Resource的JNDI的名字type="javax.sql.DataSource" maxActive="100"  //maxActive表示处于活动状态的数据库连接的最大数目,取0表示不受限制,maxIdle="30" //maxIdle表示闲置的最大数目maxWait="10000"//数据库连接处于空闲状态的等待时间,取-1表示可以无限制等待username="huyao" password="123"driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"url="jdbc:sqlserver://localhost:1433;DatabaseName=message" /></Context>2.配置所在项目里面的webroot/web-info/web.xml文件<resource-ref>  <description>message DataSource</description>//对所引用的资源的说明  <res-ref-name>jdbc/message</res-ref-name> //指定所引用的资源的JNDI的名字,与<Resource>中的那么对应  <res-type>javax.sql.DataSource</res-type> //指定所引用的资源的类名字,与<Resource>中的type对应  <res-auth>Container</res-auth> //指定管理所引用资源的Manager,与<Resource>中的auth对应  </resource-ref>3.最后在dao中建立connectionContext ctx=new InitialContext();DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/message"); //java:comp/env/前缀加上数据源名称Connection conn=ds.getConnection();


* 2.hrp.xml中配置
1.tomcat中的配置文件(tomcat\conf\catalina\localhost\hrp.xml)<?xml version='1.0' encoding='utf-8'?><Context displayName="hrp" docBase="E:\workspace_weiquan\hrp_weiquan\web\hrp" path="/hrp" reloadable="true" workDir="E:\workspace_weiquan\hrp_weiquan\work"><Resource name="jdbc/gs"   auth="Container"  type="javax.sql.DataSource"  maxActive="20"  maxIdle="30"  maxWait="5000"  username="sa"  password="12345"  driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"  url="jdbc:sqlserver://localhost:1433;selectMethod=cursor;databaseName=HRP_ACT">  </Resource></Context>2.各项属性说明name:表示你的连接池的名称也就是你要访问连接池的地址auth:是连接池管理权属性Container表示容器管理type:是对象的类型driverClassName:是数据库驱动的名称url:是数据库的地址username:是登陆数据库的用户名password:是登陆数据库的密码maxIdle,最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。MaxActive,连接池的最大数据库连接数。设为0表示无限制。maxWait ,最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。3.建立连接(WEB-INF\web.xml)<resource-ref><description>Allow</description><res-ref-name>jdbc/gs</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>4.各项属性说明description: 对所引用的资源的说明res-ref-name:指定所引用的资源的JNDI的名字,与<Resource>中的那么对应res-type:指定所引用的资源的类名字,与<Resource>中的type对应res-auth:指定管理所引用资源的Manager,与<Resource>中的auth对应5.dao中建立connection Context ctx=new InitialContext(); DataSource source=(DataSource)ctx.lookup("java:comp/env/jdbc/gs"); Connection conn=source.getConnection();


原创粉丝点击