2016-09-22 8 views
1

Calc Runnerクラスがあります。私たちは4スレッドプールでExcutiveServiceでマルチスレッドを実現しclass.Like UTIL DBStaticからこの `接続プーリングを使用してMulti_threadingを実装する方法

try { 
      synchronized (LOCK_OBJCT) { 
       if (_conn == null || _conn.isClosed()) { 
        Class.forName(DB_DRIVER); 
        logger.debug("Connecting to: " + DB_URL + "; as: " + DB_USERID); 
        _conn = DriverManager.getConnection(DB_URL, DB_USERID, DB_PASSWORD); 
        _conn.setAutoCommit(false); 
       } 
       else { 
        logger.debug("Connected to: " + DB_URL + "; as: " + DB_USERID); 
       } 
      } 
     } catch(ClassNotFoundException ce) { 
      logger.error("Error when obtaining JDBC driver.Exiting...", ce); 
      System.exit(1); 
     } catch(SQLException e) { 
      logger.error("Error when obtaining insight db conn: " + DB_URL + "; as: " + DB_USERID + " Exiting..." , e); 
      System.exit(1); 
     } 

     return _conn; 
    }` 

を取得このメソッド接続で

 for(long companyId : companies){ 
method1(); 
method2(); 
method3(); 
method4(); 
} 

。すべてのメソッドはスレッドで実行されます。 が出て接続が正常にこのマルチスレッド

+0

私は実際に 'java-ee'を使用していますが、サーバーはこれをすべて管理しています。設定が必要です。この特定のホイールを再発明する必要はありません –

答えて

0

あなたがプールされた接続を作成し、接続制限を指定する必要があります接続プーリングのためのJDBCプログラムを実装するために.how作品されていないマルチスレッドをプールに問題が何であるかを知っています。接続プールを実現することができます。

PooledConnectionDataSource ds = new PooledConnectionDataSource(); 
ds.setDescription("Oracle Data Source"); 
// Refer to a previously registered pooled data source to access 
// a ConnectionPoolDataSource object 
ds.setDataSourceName("jdbc/ConnectOracle"); 
// The pool manager will be initiated with 5 physical connections 
ds.setInitialPoolSize(5); 
// The pool maintenance thread will make sure that there are 5 
// physical connections available 
ds.setMinPoolSize(5); 
// The pool maintenance thread will check that there are no more 
// than 10 physical connections available 
ds.setMaxPoolSize(10); 
// The pool maintenance thread will wake up and check the pool 
// every 20 seconds 
ds.setPropertyCycle(20); 
// The pool maintenance thread will remove physical connections 
// that are inactive for more than 300 seconds 
ds.setMaxIdleTime(300); 
// Set tracing off since we choose not to see output listing 
// of activities on a connection 
ds.setTracing(false); 
+0

マルチスレッド環境では、接続を閉じる間にconnectのような例外が発生します.Jdbc3PoolingDataSourceで実装されています。 –

関連する問題