tomcat 6.0內建DBCP串連池,7.0已經換了新的串連池。
1、context.xml 中加入如下內容:
tomcat就會在啟動的時候載入串連池。這裡用到JNDI,它可以把DataSource對象放在一個tomcat容器中(JNDI容器),並為容器中的Datasource對象取一個名稱。
以後程式想獲得DataSource對象,之需要通過名稱檢索即可。
這裡我起的名字是:jdbc/mysql
<Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> --><Loader delegate="true" /> <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="50" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/chatRoom"/></Context>
2、然後在項目的web.xml中配置如下:
注意:只要name對應即可。
<resource-ref> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
至此,tomcat的配置已經完成。
3、在程式中載入。我寫了一個工具類來載入。
import javax.naming.Context;import javax.naming.InitialContext;import javax.sql.DataSource;public class JDBCPoolUtil { private static Context context; private static DataSource ds; //拒絕new一個執行個體 private JDBCPoolUtil() {}; static {//註冊驅動 try { context = new InitialContext(); //首碼是:java:comp/env/ (Tomcat規定) ds = (DataSource) context.lookup("java:comp/env/jdbc/mysql"); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { conn = ds.getConnection(); conn.setAutoCommit(false);} catch (SQLException e) {e.printStackTrace();} return conn; } //釋放資源 public static void free(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } } } }
注意一點,只能在servlet 或 jsp中來擷取串連。
因為這是在tomcat容器內的。如果在容器外調用,就會有如下異常:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at util.JDBCPoolUtil.<clinit>(JDBCPoolUtil.java:25)
at dao.impl.UserDao.addUser(UserDao.java:14)
at test.UserTest.main(UserTest.java:22)
具體的解決辦法我還沒找到~
如果要測試的話,建議還是寫到把測試寫到jsp裡面吧。
mysql資料來源的使用:
MysqlDataSource source = new MysqlDataSource();source.setServerName("localhost");source.setDatabaseName("testdb");source.setPort(3306);source.setUser("root");source.setPassword("root");Connection conn = source.getConnection();