📄 connect the enterprise with the jca, part 2.htm
字号:
Exception ex, Object
connectionHandle)<BR> {<BR> System.out.println("In
MyManagedConnection.sendEvent,2
");<BR> myListener.sendEvent(eventType, ex,
connectionHandle);<BR> }<BR><BR> void
removeMyConnection(MyConnection
myCon)<BR> {<BR> System.out.println("In
MyManagedConnection.removeMyConnection");<BR> connectionSet.remove(myCon);<BR> }<BR><BR> void
addMyConnection(MyConnection
myCon)<BR> {<BR> System.out.println("In
MyManagedConnection.addMyConnection");<BR> connectionSet.add(myCon);<BR> }<BR><BR> ManagedConnectionFactory
getManagedConnectionFactory()<BR> {<BR> System.out.println("In
MyManagedConnection.getManagedConnectionFactory");<BR> return
mcf;<BR> }<BR><BR>}<BR></CODE>
<P><STRONG>MyConnectionEventListener </STRONG><BR>For its part, the
<CODE>MyConnectionEventListener</CODE> class allows the application server to
register callbacks for the adapter. The application server can then perform
operations, connection-pool maintenance, for example, based on the connection
state:
<P><CODE>
<P><BR>package myjca;<BR><BR>import java.util.Vector;<BR>import
javax.resource.spi.ConnectionEvent;<BR>import
javax.resource.spi.ConnectionEventListener;<BR>import
javax.resource.spi.ManagedConnection;<BR><BR>public class
MyConnectionEventListener<BR> implements
javax.sql.ConnectionEventListener<BR>{<BR> private Vector
listeners;<BR> private ManagedConnection
mcon;<BR><BR><BR> public
MyConnectionEventListener(ManagedConnection
mcon)<BR> {<BR> System.out.println("In
MyConnectionEventListener");<BR> this.mcon
= mcon;<BR> }<BR><BR> public void
sendEvent(int eventType, Exception ex, Object
connectionHandle)<BR> {<BR> System.out.println("In
MyConnectionEventListener.sendEvent");<BR> }<BR><BR> public
void addConnectorListener(ConnectionEventListener
l)<BR> {<BR> System.out.println("In
MyConnectionEventListener.addConnectorListener");<BR> }<BR><BR> public
void removeConnectorListener(ConnectionEventListener
l)<BR> {<BR> System.out.println("In
MyConnectionEventListener.removeConnectorListener");<BR> }<BR><BR> public
void connectionClosed(javax.sql.ConnectionEvent
connectionevent)<BR> {<BR> System.out.println("In
MyConnectionEventListener.connectorClosed");<BR> }<BR><BR> public
void connectionErrorOccurred(javax.sql.ConnectionEvent
event)<BR> {<BR> System.out.println("In
MyConnectionEventListener.connectorErrorOccurred");<BR> }<BR><BR>}<BR></CODE>
<P><STRONG>MyConnectionMetaData </STRONG><BR>The
<CODE>MyConnectionMetaData</CODE> class provides meta information -- product
name, the maximum number of connections allowed, and so on -- regarding the
managed connection and the underlying physical connection class:
<P><CODE>
<P><BR>package myjca;<BR><BR>import javax.resource.ResourceException;<BR>import
javax.resource.spi.*;<BR><BR><BR>public class
MyConnectionMetaData<BR> implements
ManagedConnectionMetaData<BR>{<BR><BR> private
MyManagedConnection mc;<BR><BR> public
MyConnectionMetaData(MyManagedConnection
mc)<BR> {<BR> System.out.println("In
MyConnectionMetaData.constructor");<BR> this.mc
= mc;<BR> }<BR><BR> public String
getEISProductName()<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyConnectionMetaData.getEISProductName");<BR> return
"myJCA";<BR> }<BR><BR> public
String
getEISProductVersion()<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyConnectionMetaData.getEISProductVersion");<BR> return
"1.0";<BR><BR> }<BR><BR> public
int
getMaxConnections()<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyConnectionMetaData.getMaxConnections");<BR> return
5;<BR> }<BR><BR> public String
getUserName()<BR> throws
ResourceException<BR> {<BR>
return mc.getUserName();<BR> }<BR><BR>}<BR></CODE>
<P><STRONG>MyConnection </STRONG><BR>Next up: the <CODE>MyConnection</CODE>
class, which represents the underlying physical connection to the EIS.
<CODE>MyConnection</CODE> is one of the few classes that does not implement an
interface in the JCA specification. The implementation below is simplistic, but
a working implementation might contain connectivity code using sockets, as well
as other functionality:
<P><CODE>
<P><BR>package myjca;<BR><BR>public class
MyConnection<BR>{<BR> private MyManagedConnection
mc;<BR><BR> public MyConnection(MyManagedConnection
mc)<BR> {<BR> System.out.println("In
MyConnection");<BR> this.mc =
mc;<BR> }<BR>}<BR></CODE>
<P><STRONG>MyConnectionRequestInfo </STRONG><BR>The
<CODE>MyConnectionRequestInfo</CODE> class contains the data (such as the user
name, password, and other information) necessary to establish a connection:
<P><CODE>
<P><BR>package myjca;<BR><BR>import
javax.resource.spi.ConnectionRequestInfo;<BR><BR>public class
MyConnectionRequestInfo<BR> implements
ConnectionRequestInfo<BR>{<BR><BR> private String
user;<BR> private String
password;<BR><BR> public MyConnectionRequestInfo(String
user, String
password)<BR> {<BR> System.out.println("In
MyConnectionRequestInfo");<BR> this.user
= user;<BR> this.password =
password;<BR> }<BR><BR> public
String
getUser()<BR> {<BR> System.out.println("In
MyConnectionRequestInfo.getUser");<BR> return
user;<BR> }<BR><BR> public String
getPassword()<BR> {<BR> System.out.println("In
MyConnectionRequestInfo.getPassword");<BR> return
password;<BR> }<BR><BR> public
boolean equals(Object
obj)<BR> {<BR> System.out.println("In
MyConnectionRequestInfo.equals");<BR> if(obj
==
null)<BR> return
false;<BR> if(obj instanceof
MyConnectionRequestInfo)<BR> {<BR> MyConnectionRequestInfo
other =
(MyConnectionRequestInfo)obj;<BR> return
isEqual(user, other.user) && isEqual(password,
other.password);<BR> }
else<BR> {<BR> return
false;<BR> }<BR> }<BR><BR> public
int
hashCode()<BR> {<BR> System.out.println("In
MyConnectionRequestInfo.hashCode");<BR> String
result = "" + user +
password;<BR> return
result.hashCode();<BR> }<BR><BR> private
boolean isEqual(Object o1, Object
o2)<BR> {<BR> System.out.println("In
MyConnectionRequestInfo.isEqual");<BR> if(o1
==
null)<BR> return
o2 ==
null;<BR> else<BR> return
o1.equals(o2);<BR> }<BR><BR>}<BR></CODE>
<P><STRONG>MyDataSource </STRONG><BR>The <CODE>MyDataSource</CODE> class serves
as a connection factory for the underlying connections. Because the sample
adapter does not implement the CCI interfaces, it implements the
<CODE>DataSource</CODE> interface in the <CODE>javax.sql</CODE> package:
<P><CODE>
<P><BR>package myjca;<BR><BR>import java.io.PrintWriter;<BR>import
java.io.Serializable;<BR>import java.sql.*;<BR>import
javax.naming.Reference;<BR>import javax.resource.Referenceable;<BR>import
javax.resource.ResourceException;<BR>import
javax.resource.spi.ConnectionManager;<BR>import
javax.resource.spi.ManagedConnectionFactory;<BR>import
javax.sql.DataSource;<BR><BR><BR>public class
MyDataSource<BR> implements DataSource, Serializable,
Referenceable<BR>{<BR><BR> private String
desc;<BR> private ManagedConnectionFactory
mcf;<BR> private ConnectionManager
cm;<BR> private Reference
reference;<BR><BR><BR> public
MyDataSource(ManagedConnectionFactory mcf, ConnectionManager
cm)<BR> {<BR> System.out.println("In
MyDataSource");<BR> this.mcf =
mcf;<BR><BR> if(cm ==
null)<BR> this.cm
= new
MyConnectionManager();<BR> else<BR> this.cm
= cm;<BR> }<BR><BR> public
Connection
getConnection()<BR> throws
SQLException<BR> {<BR> System.out.println("In
MyDataSource.getConnection,1");<BR> try<BR> {<BR> return
(Connection)cm.allocateConnection(mcf,
null);<BR> }<BR> catch(ResourceException
ex)<BR> {<BR> throw
new
SQLException(ex.getMessage());<BR> }<BR> }<BR><BR> public
Connection getConnection(String username, String
password)<BR> throws
SQLException<BR> {<BR> System.out.println("In
MyDataSource.getConnection,2");<BR> try<BR> {<BR> javax.resource.spi.ConnectionRequestInfo
info = new MyConnectionRequestInfo(username,
password);<BR> return
(Connection)cm.allocateConnection(mcf,
info);<BR> }<BR> catch(ResourceException
ex)<BR> {<BR> throw
new
SQLException(ex.getMessage());<BR> }<BR> }<BR><BR> public
int getLoginTimeout()<BR> throws
SQLException<BR> {<BR> return
DriverManager.getLoginTimeout();<BR> }<BR><BR> public
void setLoginTimeout(int
seconds)<BR> throws
SQLException<BR> {<BR> DriverManager.setLoginTimeout(seconds);<BR> }<BR><BR> public
PrintWriter
getLogWriter()<BR> throws
SQLException<BR> {<BR> return
DriverManager.getLogWriter();<BR> }<BR><BR> public
void setLogWriter(PrintWriter
out)<BR> throws
SQLException<BR> {<BR> DriverManager.setLogWriter(out);<BR> }<BR><BR> public
String
getDescription()<BR> {<BR> return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -