/coderworld/p/database-connectionpool.html 阅读目录 引言 设计 使用 代码 测试 回到顶部 引言 池化技术在Java中应用的很广泛,简而论之,使用对象池存储某个实例数受限制的实例,开发者从对象池中获取实例,使用完之后再换回对
java配置数据库连接池以mysql和sqlserver为例
配置tomcat下面的content.xml
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/com?useUnicode=true&characterEncoding=UTF-8"
username="root"
password="root"
maxActive="100"
maxIdle="2"
maxWait="10000" />
removeAbandoned
true
removeAbandonedTimeout
60
logAbandoned
true
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=fashion"
username="sa"
password=""
maxActive="100"
maxIdle="30"
maxWait="10000" />
removeAbandoned
true
removeAbandonedTimeout
60
logAbandoned
true
-->
WEB-INF/web.xml
设置项目目录下面的WEB-INF下面的web.xml文件在里面加入META-INF文件下新建context.xml,里面的内容为:
DB Connection
jdbc/mysql
javax.sql.DataSource
Container
DB
jdbc/sqlserver
javax.sql.DataSource
Container
测试文件
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
DataSource ds = null;
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sqlserver");
Connection conn = ds.getConnection();
%>
Statement stmt=conn.createStatement();
String sql_gun="select * from sm_t_salesheet" ;
ResultSet rs_gun=stmt.executeQuery(sql_gun);
while(rs_gun.next())
{
%>
}
rs_gun.close();
stmt.close();
%>
DataSource ds2 = null;
InitialContext ctx2=new InitialContext();
ds2=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn2 = ds2.getConnection();
%>
Statement stmt2=conn2.createStatement();
String sql_gun2="select * from sm_t_salesheet" ;
ResultSet rs_gun2=stmt2.executeQuery(sql_gun2);
while(rs_gun2.next())
{
%>
}
rs_gun2.close();
stmt2.close();
%>