tomcat 6.0 串連池的配置

來源:互聯網
上載者:User

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();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.