tomcat7连接池使用
jopen
9年前
一直纠结于数据库连接池的实现,今天终于挤出时间来实现一下。tomcat之前连接池使用dbcp实现,dbcp有很多的缺点,现在的tomcat采用了自己实现的线程池,用起来还是非常的简单方面的。整体操作如下:
1 配置连接池
将以下内容保存为context.xml,放在web项目下的META-INF文件夹下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <Context> <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" maxActive="100" //最大激活连接 maxIdle="30" //最大空闲连接 maxWait="1000" //最大等待数 username="test" //数据库用户名 password="testtest" //数据库密码 driverClassName="com.mysql.jdbc.Driver" //此处使用mysql数据库,请自行修改驱动 url="jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8" /> //test为数据库名,编码为UTF-8 </Context>
2 代码中引用连接池
package com.hrb2c.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.concurrent.Future; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.tomcat.jdbc.pool.DataSource; /** * 数据库连接池 * * @author Yuedong Li * */ @WebServlet("/ConnPoolTest") public class ConnPoolTest extends HttpServlet { private static final long serialVersionUID = 1L; private Connection con = null; private static DataSource datasource; /** * @see HttpServlet#HttpServlet() */ public ConnPoolTest() { super(); try { //获取数据源 datasource = getInstance(); //连接池同步 Future<Connection> future = datasource.getConnectionAsync(); while (!future.isDone()) { // 等待连接池同步 Thread.sleep(100); } // 获取连接池 con = future.get(); } catch (Exception e) { e.printStackTrace(); } } /** * 单例模式获取数据源 * @return * @throws NamingException */ private DataSource getInstance() throws NamingException { if (datasource == null) { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); datasource = (DataSource) envContext.lookup("jdbc/test"); } return datasource; } /** * 处理get方法 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * 处理post方法 */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Statement st; StringBuffer buff = new StringBuffer("<html><body>"); try { if (con != null) { //简单查询,test表中有2列,id与name buff.append("id"); buff.append(" "); buff.append("name"); buff.append("<br>"); st = con.createStatement(); ResultSet rs = st.executeQuery("select * from test"); while (rs.next()) { buff.append(rs.getString("id")); buff.append(" "); buff.append(rs.getString("name")); buff.append("<br>"); } } buff.append("</body></html>"); } catch (SQLException e) { e.printStackTrace(); } PrintWriter out = response.getWriter(); out.write(buff.toString()); } }