例: 並行性と接続の使用

Runnable または Callable は、それが作成するコンポーネントが java:comp リソース参照を使用して取得した接続を使用できます。

リソース参照について詳しくは、『参照』トピックを参照してください。以下に、接続を正しく使用しているタスクの例を示します。

class GoodTask implements Callable<Object> 
{
   	DataSource ds;
   public Task() throws NamingException
   {   
      // Cache a connection factory or datasource
      		// as class instance data.
      InitialContext ic = new InitialContext();
      // It is assumed that the created Java EE component has this
      		// resource reference defined in its deployment descriptor.
      		ds = (DataSource)ic.lookup("java:comp/env/jdbc/myDataSource");
   }
   	// When the asynchronous bean method is called, get a connection,
   	//  use it, then close it.
   public Object call() throws SQLException
   {
      try (Connection c = ds.getConnection()) {
             // Use the connection now.
             return someResult;
      }
   }
} 

以下に、接続の使い方が正しくないタスクの例を示します。

class BadTask implements Callable<Object> 
{
   	DataSource ds;
   	// Do not do this. You cannot cache connections across method calls.
   	Connection c;

   public BadTask() throws NamingException, SQLException
   {
      // Cache a connection factory or datasource as
      		// class instance data.
      InitialContext ic = new InitialContext();
      		ds = (DataSource)ic.lookup("java:comp/env/jdbc/myDataSource");
      // Here, you broke the rules.
      			c = ds.getConnection();
   }

   // Now when the method is called, illegally use the cached connection
   	// and you likely see J2C related exceptions at run time.
   public Object call()
   {
      // Use the connection now.
      return someResult;
   }
} 

トピックのタイプを示すアイコン 参照トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=xasb_connections
ファイル名:xasb_connections.html