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.naming.NamingException ; 13 import javax.naming.Reference ; 14 import javax.naming.Referenceable ; 15 import javax.naming.StringRefAddr ; 16 import javax.sql.DataSource ; 17 import javax.sql.XAConnection ; 18 import javax.sql.XADataSource ; 19 import javax.transaction.Status ; 20 import javax.transaction.SystemException ; 21 import javax.transaction.Transaction ; 22 import javax.transaction.TransactionManager ; 23 24 import org.jfox.ioc.common.AbstractService; 25 import org.jfox.ioc.ext.ActiveComponent; 26 import org.jfox.jndi.InitialContextHelper; 27 import org.jfox.tm.TxManager; 28 29 35 36 public class TxDataSource extends AbstractService 37 implements DataSource , 38 XADataSource , 39 Referenceable , 40 ActiveComponent { 41 private String dsName = null; 43 private String dbUrl = null; 44 private String user = null; 45 private String password = null; 46 private int initNum = 2; 47 private int maxRest = 10; 48 private long timeout = 10 * 60 * 1000; private int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED; 50 51 private XAConnectionPool pool = null; 53 private static TransactionManager tm = null; 54 55 59 public TxDataSource(String dsName, String dbUrl, String user, String password) { 60 this.dsName = dsName; 61 this.dbUrl = dbUrl; 62 this.user = user; 63 this.password = password; 64 } 65 66 69 TxDataSource(String dbUrl, String user, String password) { 70 this.dbUrl = dbUrl; 71 this.user = user; 72 this.password = password; 73 } 74 75 public int getLoginTimeout() throws SQLException { 76 return ((XAConnectionFactory) pool.getObjectFactory()).getLoginTimeout(); 77 } 78 79 public void setLoginTimeout(int seconds) throws SQLException { 80 ((XAConnectionFactory) pool.getObjectFactory()).setLoginTimeout(seconds); 81 } 82 83 public PrintWriter getLogWriter() throws SQLException { 84 return ((XAConnectionFactory) pool.getObjectFactory()).getLogWriter(); 85 } 86 87 public void setLogWriter(PrintWriter out) throws SQLException { 88 ((XAConnectionFactory) pool.getObjectFactory()).setLogWriter(out); 89 } 90 91 public Connection getConnection() throws SQLException { 92 return getConnection(user, password); 93 } 94 95 public Connection getConnection(String username, String password) throws SQLException { 96 return getXAConnection(username, password).getConnection(); 97 } 98 99 public XAConnection getXAConnection() throws SQLException { 100 return getXAConnection(user, password); 101 } 102 103 public XAConnection getXAConnection(String user, String password) throws SQLException { 104 if(pool == null) throw new SQLException ("please run init() to build connection pool first."); 105 106 int txStatus = Status.STATUS_NO_TRANSACTION; 109 Transaction tran = null; 110 try { 111 txStatus = tm.getStatus(); 112 if(txStatus == Status.STATUS_NO_TRANSACTION) { 114 throw new SQLException ("can not getXAConnection while current thread is not in Transaction context!"); 115 } 116 tran = tm.getTransaction(); 117 if(XAConnectionManager.isAssociated(tran, dbUrl, user, password)) { 118 return XAConnectionManager.getXAConnection(tran, dbUrl, user, password); 119 } 120 } 121 catch(SystemException e) { 122 logger.error("getXAConnection associate current transaction error", e); 123 } 124 125 XAConnection xaconn = null; 126 try { 127 if(!user.equals(this.user) || !password.equals(this.password)) { 129 xaconn = (XAConnection ) pool.retrieveObject(user, password); 130 } 131 else { 132 xaconn = (PoolableXAConnection) pool.retrieveObject(); 133 } 134 } 135 catch(Exception e) { 136 logger.warn("retrieve object exception ", e); 137 if(e instanceof SQLException ) { 138 throw (SQLException ) e; 139 } 140 else { 141 throw new SQLException (e.getMessage()); 142 } 143 } 144 return xaconn; 145 } 146 147 public Reference getReference() throws NamingException { 148 Reference ref = new Reference (getClass().getName(), XADataSourceObjectFactory.class.getName(), null); 149 ref.add(new StringRefAddr ("user", getUser())); 150 ref.add(new StringRefAddr ("password", getPassword())); 151 ref.add(new StringRefAddr ("dbURL", getDbUrl())); 152 ref.add(new StringRefAddr ("dsName", getDsName())); 154 return ref; 155 } 156 157 public String getDsName() { 158 return dsName; 159 } 160 161 public String getDbUrl() { 162 return dbUrl; 163 } 164 165 public String getUser() { 166 return user; 167 } 168 169 public String getPassword() { 170 return password; 171 } 172 173 public void setTimeout(long timeout) { 174 this.timeout = timeout; 175 } 176 177 public long getTimeout() { 178 return timeout; 179 } 180 181 public int getMaxRest() { 182 return maxRest; 183 } 184 185 public int getInitNum() { 186 return initNum; 187 } 188 189 public void setInitNum(int initNum) { 190 this.initNum = initNum; 191 } 192 193 public void setMaxRest(int maxRest) { 194 this.maxRest = maxRest; 195 } 196 197 public void setTransactionIsolation(int level) { 198 transactionIsolation = level; 199 } 200 201 public int getTransactionIsolation() { 202 return transactionIsolation; 203 } 204 205 static TransactionManager getTransactionManager() { 206 return tm; 207 } 208 209 protected void doInit() throws Exception { 210 if(tm == null) { 211 try { 212 tm = TxManager.getInstance(); 213 } 214 catch(Exception e) { 215 throw new RuntimeException (e); 216 } 217 } 218 XAConnectionFactory xaConnFactory = new XAConnectionFactory(dbUrl, user, password); 219 xaConnFactory.setTransactionIsolation(transactionIsolation); 220 pool = new XAConnectionPool(xaConnFactory, initNum, maxRest, timeout); 221 pool.init(); 222 } 223 224 protected void doDestroy() throws Exception { 225 pool.destroy(); 226 pool = null; 227 XADataSourceManager.unregisterDataSource(dsName); 228 } 229 230 protected void doStart() throws Exception { 231 InitialContextHelper.getInitialContext().rebind(getDsName(), this); 232 } 233 234 protected void doStop() throws Exception { 235 InitialContextHelper.getInitialContext().unbind(getDsName()); 236 } 237 238 public void run() { 239 } 240 241 public static void main(String [] args) { 242 243 } 244 } 245 | Popular Tags |