import javax.resource.ResourceException; import javax.resource.spi.ConnectionManager; import javax.resource.spi.ManagedConnectionFactory; public class MyResourceAdapter { public Connection getConnection() throws ResourceException { ConnectionManager connectionManager = new MyConnectionManager(); ManagedConnectionFactory mcf = new MyManagedConnectionFactory(); return (Connection) connectionManager.allocateConnection(mcf, null); } } public class MyConnectionManager implements ConnectionManager { @Override public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo) throws ResourceException { } } public class MyManagedConnectionFactory implements ManagedConnectionFactory { @Override public Object createConnectionFactory(ConnectionManager cxManager) throws ResourceException { } } public interface Connection { void sendRequest(String request); String receiveResponse(); } <?xml version="1.0" encoding="UTF-8"?> <connector xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/connector_1_5.xsd" xmlns="http://java.sun.com/xml/ns/j2ee"> <resourceadapter> <resourceadapter-class>com.example.MyResourceAdapter</resourceadapter-class> <config-property> <description>Database URL</description> <config-property-name>URL</config-property-name> <config-property-type>java.lang.String</config-property-type> <config-property-value>jdbc:mysql://localhost/mydatabase</config-property-value> </config-property> <config-property> <description>Database Username</description> <config-property-name>username</config-property-name> <config-property-type>java.lang.String</config-property-type> <config-property-value>myusername</config-property-value> </config-property> <config-property> <description>Database Password</description> <config-property-name>password</config-property-name> <config-property-type>java.lang.String</config-property-type> <config-property-value>mypassword</config-property-value> </config-property> </resourceadapter> </connector> import javax.naming.InitialContext; import javax.resource.spi.ConnectionManager; import javax.resource.spi.ManagedConnectionFactory; InitialContext initialContext = new InitialContext(); MyResourceAdapter resourceAdapter = (MyResourceAdapter) initialContext.lookup("java:comp/env/MyResourceAdapter"); Connection connection = resourceAdapter.getConnection();


上一篇:
下一篇:
切换中文