一直糾結於資料庫連接池的實現,今天終於擠出時間來實現一下。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與namebuff.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());}}
這樣就可以運行起來了,運行後可能會出現以下異常:
java.sql.SQLException: com.mysql.jdbc.Driver
只需要將mysql的jdbc驅動的jar包放進tomcat的lib目錄裡就可以了,注意是tomcat的目錄下,不是項目的目錄下。
第一次配置,難免有敘述錯誤之處,望大神們指正。