1 6 7 package org.jfox.jdbc.xa; 8 9 import java.io.PrintWriter ; 10 import java.sql.Connection ; 11 import java.sql.SQLException ; 12 import javax.sql.XAConnection ; 13 import javax.sql.XADataSource ; 14 15 import org.jfox.ioc.logger.Logger; 16 import org.jfox.pool.ObjectFactory; 17 import org.jfox.pool.PoolableObject; 18 19 24 25 public class XAConnectionFactory implements ObjectFactory { 26 private XADataSource xads = null; 27 28 private String url = ""; 29 private String user = ""; 30 private String password = ""; 31 private int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED; 32 33 private XAConnectionPool pool = null; 34 35 private static Logger logger = Logger.getLogger(XAConnectionFactory.class.getName()); 36 37 public XAConnectionFactory(String url, String user, String password) { 38 this.url = url; 39 this.user = user; 40 this.password = password; 41 try { 42 xads = createXADataDataSource(); 44 } 45 catch(Exception e) { 46 e.printStackTrace(); 47 throw new RuntimeException ("create xadatasource error", e); 48 } 49 } 50 51 String getUser() { 52 return user; 53 } 54 55 String getPassword() { 56 return password; 57 } 58 59 String getDbURL() { 60 return url; 61 } 62 63 public Class getObjectClass() { 64 return XAConnection .class; 65 } 66 67 public PoolableObject makeObject() throws Exception { 68 logger.debug("make object"); 69 PoolableXAConnection poolxaconn = new PoolableXAConnection(getXAConnection(user, password), transactionIsolation, url, user, password); 70 poolxaconn.setPool(pool); 71 return poolxaconn; 72 } 73 74 77 PoolableObject makeObject(String user, String password) throws Exception { 78 logger.debug("make object by " + user + " & " + password); 79 PoolableXAConnection pxaconn = new PoolableXAConnection(getXAConnection(user, password), transactionIsolation, url, user, password); 80 pxaconn.notPoolable(); 81 return pxaconn; 82 } 83 84 public void destroyObject(PoolableObject object) throws Exception { 85 logger.debug("destroyObject " + object); 86 PoolableXAConnection poolXaConn = (PoolableXAConnection) object; 87 poolXaConn.close(); 88 poolXaConn = null; 89 } 90 91 98 public boolean validateObject(PoolableObject object) { 99 boolean isAvailable = PoolableXAConnection.class.isAssignableFrom(object.getClass()) && object.isAvailable(); 100 logger.debug("validate object " + object + " " + isAvailable); 101 return isAvailable; 102 } 103 104 XAConnection getXAConnection(String user, String password) throws SQLException { 105 return xads.getXAConnection(user, password); 106 } 107 108 int getLoginTimeout() throws SQLException { 109 return xads.getLoginTimeout(); 110 } 111 112 void setLoginTimeout(int seconds) throws SQLException { 113 xads.setLoginTimeout(seconds); 114 } 115 116 PrintWriter getLogWriter() throws SQLException { 117 return xads.getLogWriter(); 118 } 119 120 void setLogWriter(PrintWriter out) throws SQLException { 121 xads.setLogWriter(out); 122 } 123 124 void setPool(XAConnectionPool pool) { 125 this.pool = pool; 126 } 127 128 void setTransactionIsolation(int level) throws SQLException { 129 transactionIsolation = level; 130 } 131 132 private XADataSource createXADataDataSource() throws Exception { 133 String protocol = getProtocol(); 134 ClassLoader loader = this.getClass().getClassLoader(); 135 String className = this.getClass().getPackage().getName() + "." + protocol.toUpperCase() + "XADataSource"; 136 Class clz = loader.loadClass(className); 137 AbstractXADataSource xadsf = (AbstractXADataSource)clz.getConstructor(new Class []{String .class}).newInstance(new Object []{url}); 138 return xadsf; 139 } 140 141 private String getProtocol() { 142 int index1 = url.indexOf(":"); 143 int index2 = url.indexOf(":", index1 + 1); 144 String protocol = url.substring(index1 + 1, index2); 145 return protocol; 146 } 147 148 public static void main(String [] args) { 149 150 } 151 } 152 | Popular Tags |