Home > Database > Mysql Tutorial > body text

Mysql Jdbc数据库连接池配置_MySQL

WBOY
Release: 2016-06-01 13:51:35
Original
1218 people have browsed it

Reference:《【问题】数据库连接池》

网上好多配置方法,我只记录一下自己配置成功的

1.在自己的WEB应用的META-INF文件夹下新建context.xml 内容如下

<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"><?xml version="1.0" encoding="UTF-8"?>
Copy after login
<context path="/appName" docbase="appName"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  debug="5" crossContext="true" reloadable="false"  
Copy after login
  cachingAllowed="true" cacheMaxSize="20480"  
Copy after login
  cacheTTL="10000">  
Copy after login
  <resource name="jdbc/mysql" auth="Container" removeabandoned="true"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">  removeAbandonedTimeout="60" logAbandoned="true"  
Copy after login
  type="javax.sql.DataSource" maxActive="50" maxIdle="10"  
Copy after login
  maxWait="10000" username="root" password="root"  
Copy after login
  driverClassName="com.mysql.jdbc.Driver"  
Copy after login
  url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true" />  
Copy after login
Copy after login
Copy after login
Copy after login

注意下改参数名字

2.在web.xml中加入如下代码,声明使用外部资源(resource-ref的位置应该蛮靠后的,我是扔在最后面了)

<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb"><resource-ref></resource-ref>
Copy after login
	<description>DB Connection </description>
Copy after login
	<res-ref-name>jdbc/mysql</res-ref-name>
Copy after login
	<res-type>javax.sql.DataSource</res-type>
Copy after login
	<res-auth>Container</res-auth>
Copy after login
Copy after login
Copy after login
Copy after login

 

3.把JDBC驱动包甩到WEB的lib下面(我的是mysql-connector-java-5.1.15-bin.jar)

 

重启Server就OK 了,使用的时候这样获取Connection

<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb">Context context = new InitialContext();
Copy after login
DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/mysql");
Copy after login
conn = ds.getConnection();
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template