作者:mobiledu2502872877 | 来源:互联网 | 2018-04-16 12:53
Reference:《【问题】数据库连接池》
网上好多配置方法,我只记录一下自己配置成功的
1.在自己的WEB应用的META-INF文件夹下新建context.xml 内容如下
debug="5" crossCOntext="true" reloadable="false"
cachingAllowed="true" cacheMaxSize="20480"
cacheTTL="10000">
removeAbandOnedTimeout="60" logAbandOned="true"
type="javax.sql.DataSource" maxActive="50" maxIdle="10"
maxWait="10000" username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbname?autoRecOnnect=true" />
注意下改参数名字
2.在web.xml中加入如下代码,声明使用外部资源(resource-ref的位置应该蛮靠后的,我是扔在最后面了)
DB Connection
jdbc/mysql
javax.sql.DataSource
Container
 
3.把JDBC驱动包甩到WEB的lib下面(我的是mysql-connector-java-5.1.15-bin.jar)
 
重启Server就OK 了,使用的时候这样获取Connection
Context cOntext= new InitialContext();
DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/mysql");
cOnn= ds.getConnection();