1 22 package org.jboss.resource.adapter.jdbc.local; 23 24 import java.util.Hashtable ; 25 import javax.naming.InitialContext ; 26 import javax.naming.Name ; 27 import javax.naming.NamingException ; 28 import javax.resource.ResourceException ; 29 import javax.resource.spi.ConnectionManager ; 30 import javax.resource.spi.ConnectionRequestInfo ; 31 import javax.resource.spi.ManagedConnectionFactory ; 32 import javax.transaction.TransactionManager ; 33 import org.jboss.deployment.DeploymentException; 34 import org.jboss.logging.Logger; 35 import org.jboss.resource.connectionmanager.CachedConnectionManager; 36 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 37 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 38 import org.jboss.resource.connectionmanager.TxConnectionManager; 39 import org.jboss.resource.connectionmanager.CachedConnectionManagerReference; 40 import org.jboss.util.naming.NonSerializableFactory; 41 import org.jboss.util.naming.Util; 42 43 50 public class LocalTxDataSource 51 { 52 protected static Logger log = Logger.getLogger(LocalTxDataSource.class.getName()); 53 54 55 public LocalTxDataSource() 56 { 57 } 58 59 private CachedConnectionManager cachedConnectionManager; 60 private TransactionManager transactionManager; 61 private String jndiName; 62 63 private InternalManagedConnectionPool.PoolParams poolParams = new InternalManagedConnectionPool.PoolParams(); 64 private LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory(); 65 66 private JBossManagedConnectionPool.OnePool pool = new JBossManagedConnectionPool.OnePool(mcf, poolParams, false, log); 67 private TxConnectionManager connectionManager; 68 private Object datasource; 69 protected Hashtable initialContextProperties; 70 protected InitialContext initialContext; 71 72 public class ConnectionManagerDelegate implements ConnectionManager 73 { 74 private static final long serialVersionUID = 1L; 75 76 public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo cxRequestInfo) throws ResourceException 77 { 78 return connectionManager.allocateConnection(mcf, cxRequestInfo); 79 } 80 } 81 82 public void setInitialContextProperties(Hashtable initialContextProperties) 83 { 84 this.initialContextProperties = initialContextProperties; 85 } 86 87 public void start() throws Exception 88 { 89 if (initialContextProperties == null) initialContext = new InitialContext (); 90 else initialContext = new InitialContext (initialContextProperties); 91 92 connectionManager = new TxConnectionManager(cachedConnectionManager, pool, transactionManager); 93 connectionManager.setLocalTransactions(true); 94 connectionManager.setTrackConnectionByTx(true); 95 pool.setConnectionListenerFactory(connectionManager); 96 datasource = connectionManager.getPoolingStrategy().getManagedConnectionFactory().createConnectionFactory(new ConnectionManagerDelegate()); 97 bindConnectionFactory(); 98 } 99 100 101 104 protected void bindConnectionFactory() throws Exception 105 { 106 InitialContext ctx = initialContext; 107 try 108 { 109 Name name = ctx.getNameParser("").parse(jndiName); 110 String key = name.toString(); 111 if( true == true && name.size() > 1 ) 112 { 113 int size = name.size() - 1; 114 Util.createSubcontext(initialContext, name.getPrefix(size)); 115 } 116 NonSerializableFactory.rebind(initialContext, key, datasource); 117 log.info("Bound datasource to JNDI name '" + jndiName + "'"); 118 } 119 catch (NamingException ne) 120 { 121 throw new DeploymentException("Could not bind ConnectionFactory into jndi: " + jndiName, ne); 122 } 123 finally 124 { 125 ctx.close(); 126 } 127 } 128 129 protected void unbindConnectionFactory() throws Exception 130 { 131 InitialContext ctx = initialContext; 132 try 133 { 134 ctx.unbind(jndiName); 135 NonSerializableFactory.unbind(jndiName); 136 log.info("Unbound datasource for JNDI name '" + jndiName + "'"); 137 } 138 catch (NamingException ne) 139 { 140 log.error("Could not unbind datasource from jndi: " + jndiName, ne); 141 } 142 finally 143 { 144 ctx.close(); 145 } 146 } 147 148 public Object getDatasource() 149 { 150 return datasource; 151 } 152 153 public void setCachedConnectionManager(CachedConnectionManagerReference cachedConnectionManager) 154 { 155 this.cachedConnectionManager = cachedConnectionManager.getCachedConnectionManager(); 156 } 157 158 public TransactionManager getTransactionManager() 159 { 160 return transactionManager; 161 } 162 163 public void setTransactionManager(TransactionManager transactionManager) 164 { 165 this.transactionManager = transactionManager; 166 } 167 168 public String getJndiName() 169 { 170 return jndiName; 171 } 172 173 public void setJndiName(String jndiName) 174 { 175 this.jndiName = jndiName; 176 } 177 178 public int getMinSize() 179 { 180 return poolParams.minSize; 181 } 182 183 public void setMinSize(int minSize) 184 { 185 poolParams.minSize = minSize; 186 } 187 188 public int getMaxSize() 189 { 190 return poolParams.maxSize; 191 } 192 193 public void setMaxSize(int maxSize) 194 { 195 poolParams.maxSize = maxSize; 196 } 197 198 public int getBlockingTimeout() 199 { 200 return poolParams.blockingTimeout; 201 } 202 203 public void setBlockingTimeout(int blockingTimeout) 204 { 205 poolParams.blockingTimeout = blockingTimeout; 206 } 207 208 public long getIdleTimeout() 209 { 210 return poolParams.idleTimeout; 211 } 212 213 public void setIdleTimeout(long idleTimeout) 214 { 215 poolParams.idleTimeout = idleTimeout; 216 } 217 218 public String getDriverClass() 219 { 220 return mcf.getDriverClass(); 221 } 222 223 public void setDriverClass(final String driverClass) 224 { 225 mcf.setDriverClass(driverClass); 226 } 227 228 public String getConnectionURL() 229 { 230 return mcf.getConnectionURL(); 231 } 232 233 public void setConnectionURL(final String connectionURL) 234 { 235 mcf.setConnectionURL(connectionURL); 236 } 237 238 public void setUserName(final String userName) 239 { 240 mcf.setUserName(userName); 241 } 242 243 public void setPassword(final String password) 244 { 245 mcf.setPassword(password); 246 } 247 248 public void setPreparedStatementCacheSize(int size) 249 { 250 mcf.setPreparedStatementCacheSize(size); 251 } 252 253 public int getPreparedStatementCacheSize() 254 { 255 return mcf.getPreparedStatementCacheSize(); 256 } 257 258 public boolean getSharePreparedStatements() 259 { 260 return mcf.getSharePreparedStatements(); 261 } 262 263 public void setSharePreparedStatements(boolean sharePS) 264 { 265 mcf.setSharePreparedStatements(sharePS); 266 } 267 268 public boolean getTxQueryTimeout() 269 { 270 return mcf.isTransactionQueryTimeout(); 271 } 272 273 public void setTxQueryTimeout(boolean qt) 274 { 275 mcf.setTransactionQueryTimeout(qt); 276 } 277 278 public String getTransactionIsolation() 279 { 280 return mcf.getTransactionIsolation(); 281 } 282 283 public void setTransactionIsolation(String transactionIsolation) 284 { 285 mcf.setTransactionIsolation(transactionIsolation); 286 } 287 288 public String getNewConnectionSQL() 289 { 290 return mcf.getNewConnectionSQL(); 291 } 292 293 public void setNewConnectionSQL(String newConnectionSQL) 294 { 295 mcf.setNewConnectionSQL(newConnectionSQL); 296 } 297 298 public String getCheckValidConnectionSQL() 299 { 300 return mcf.getCheckValidConnectionSQL(); 301 } 302 303 public void setCheckValidConnectionSQL(String checkValidConnectionSQL) 304 { 305 mcf.setCheckValidConnectionSQL(checkValidConnectionSQL); 306 } 307 308 public String getTrackStatements() 309 { 310 return mcf.getTrackStatements(); 311 } 312 313 public void setTrackStatements(String value) 314 { 315 mcf.setTrackStatements(value); 316 } 317 318 public String getExceptionSorterClassName() 319 { 320 return mcf.getExceptionSorterClassName(); 321 } 322 323 public void setExceptionSorterClassName(String exceptionSorterClassName) 324 { 325 mcf.setExceptionSorterClassName(exceptionSorterClassName); 326 } 327 328 public String getValidConnectionCheckerClassName() 329 { 330 return mcf.getValidConnectionCheckerClassName(); 331 } 332 333 public void setValidConnectionCheckerClassName(String value) 334 { 335 mcf.setValidConnectionCheckerClassName(value); 336 } 337 338 } 339 | Popular Tags |