WebSphere Application Server, Version 6.1   
             オペレーティング・システム: AIX , HP-UX, Linux, Solaris, Windows, Windows Vista

             目次と検索結果のパーソナライズ化

例: 非同期 Bean による接続の使用

非同期 Bean メソッドは、このメソッドが作成する Java 2 Platform Enterprise Edition (J2EE) コンポーネントが java:comp リソース参照を使用して取得した接続を使用することができます

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

class GoodAsynchBean
{
	DataSource ds;
	public GoodAsynchBean()
		throws NamingException
	{
		// ok to cache a connection factory or datasource
		// as class instance data.
InitialContext ic = new InitialContext();
		// it is assumed that the created J2EE 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.
	void anEventListener()
	{
		Connection c = null;
		try {
    			c = ds.getConnection();
			// use the connection now...
		}
		finally
		{
			if(c != null) c.close();
		}
	}
}

以下に、接続の使い方が正しくない非同期 Bean の一例を示します。

class BadAsynchBean
{
	DataSource ds;
	// Do not do this. You cannot cache connections across asynch method calls.
	Connection c;

	public BadAsynchBean()
		throws NamingException
	{
		// ok to 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 asynch method is called, illegally use the cached connection
	// and you likely see J2C related exceptions at run time.
	// close it.
	void someAsynchMethod()
	{
		// use the connection now...
	}
}



関連概念
非同期 Bean
参照
関連タスク
非同期 Bean の使用
参照トピック    

ご利用条件 | フィードバック

最終更新: Jan 21, 2008 5:05:53 PM EST
http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/asyncbns/xmp/xasb_connections.html