2011-01-18 5 views
0

私はWebアプリケーションを構築しており、付属の利点のため「接続プーリング」を使用したいと考えています。 私はいくつかのチュートリアルを読んでいますが、私は本当に何をする必要があるのか​​分かりません。Tomcat 6とMySQLで接続プールを使用するには?

誰かが私に北を与えることができたら、私は感謝します。

私はJSP/Servlet、MySQL、Tomcat 6、Netbeans 6.9.1を使用しています。

お礼、 ヴァーター・アンリケ。

答えて

3

http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Exampleはありますか? Webアプリケーションからデータベースにアクセスするためのすべての手順を示します。

あなたは(JSPからよりもはるかに良い)Javaコードをwithingからデータベースにアクセスする必要がある場合は、あなたのコードは次のようになります。また

InitialContext initCtx = new InitialContext(); 
// getting the datasource declared in web.xml 
DataSource dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/TestDB"); 

// getting a connection from the dataSOurce/connection pool 
Connection c = null; 
try { 
    c = dataSource.getConnection(); 
    // use c to get some data 
} 
finally { 
    // always close the connection in a finally block in order to give it back to the pool 
    if (c != null) { 
     try { 
      c.close(); 
     } 
     catch (SQLException e) { 
      // not much to do except perhaps log the exception 
     } 
    } 
} 

、あなたも使用結果セットと文を閉じる必要があることに注意してくださいtryブロックの内側にあります。より完全な例については、http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html#Random_Connection_Closed_Exceptionsを参照してください。

+0

ありがとうございました!それは本当に私を助けます。 –

関連する問題