1 8 9 package com.sleepycat.je.jca.ra; 10 11 import java.io.PrintWriter ; 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import javax.resource.ResourceException ; 18 import javax.resource.spi.ConnectionEvent ; 19 import javax.resource.spi.ConnectionEventListener ; 20 import javax.resource.spi.ConnectionRequestInfo ; 21 import javax.resource.spi.LocalTransaction ; 22 import javax.resource.spi.ManagedConnection ; 23 import javax.resource.spi.ManagedConnectionMetaData ; 24 import javax.security.auth.Subject ; 25 import javax.transaction.xa.XAResource ; 26 27 import com.sleepycat.je.Database; 28 import com.sleepycat.je.DatabaseConfig; 29 import com.sleepycat.je.DatabaseException; 30 import com.sleepycat.je.DbInternal; 31 import com.sleepycat.je.SecondaryConfig; 32 import com.sleepycat.je.SecondaryDatabase; 33 import com.sleepycat.je.TransactionConfig; 34 import com.sleepycat.je.XAEnvironment; 35 36 public class JEManagedConnection implements ManagedConnection { 37 private ArrayList listeners; 38 private JEConnection conn; 39 private XAEnvironment env; 40 private JELocalTransaction savedLT; 41 private TransactionConfig savedTransConfig; 42 private Map rwDatabaseHandleCache; 43 private Map roDatabaseHandleCache; 44 private Map rwSecondaryDatabaseHandleCache; 45 private Map roSecondaryDatabaseHandleCache; 46 47 JEManagedConnection(Subject subject, JERequestInfo jeInfo) 48 throws ResourceException { 49 50 try { 51 savedTransConfig = jeInfo.getTransactionConfig(); 52 this.env = new XAEnvironment(jeInfo.getJERootDir(), 53 jeInfo.getEnvConfig()); 54 } catch (DatabaseException DE) { 55 throw new ResourceException (DE.toString()); 56 } 57 listeners = new ArrayList (); 58 savedLT = null; 59 rwDatabaseHandleCache = new HashMap (); 60 roDatabaseHandleCache = new HashMap (); 61 rwSecondaryDatabaseHandleCache = new HashMap (); 62 roSecondaryDatabaseHandleCache = new HashMap (); 63 } 64 65 public Object getConnection(Subject subject, 66 ConnectionRequestInfo connectionRequestInfo) 67 throws ResourceException { 68 69 if (conn == null) { 70 conn = new JEConnection(this); 71 } 72 return conn; 73 } 74 75 protected XAEnvironment getEnvironment() 76 throws ResourceException { 77 78 return env; 79 } 80 81 public LocalTransaction getLocalTransaction() 82 throws ResourceException { 83 84 91 if (conn == null) { 92 savedLT = new JELocalTransaction(env, savedTransConfig, this); 93 return savedLT; 94 } 95 96 JELocalTransaction lt = conn.getLocalTransaction(); 97 if (lt == null) { 98 if (savedLT == null) { 99 lt = new JELocalTransaction(env, savedTransConfig, this); 100 } else { 101 lt = savedLT; 102 } 103 conn.setLocalTransaction(lt); 104 savedLT = null; 105 } 106 return lt; 107 } 108 109 public XAResource getXAResource() 110 throws ResourceException { 111 112 return (XAResource ) env; 113 } 114 115 public void associateConnection(Object connection) 116 throws ResourceException { 117 118 conn = (JEConnection) connection; 119 conn.setManagedConnection(this, savedLT); 120 savedLT = null; 121 } 122 123 public void addConnectionEventListener(ConnectionEventListener listener) { 124 listeners.add(listener); 125 } 126 127 public void 128 removeConnectionEventListener(ConnectionEventListener listener) { 129 130 listeners.remove(listener); 131 } 132 133 public ManagedConnectionMetaData getMetaData() 134 throws ResourceException { 135 136 return new JEConnectionMetaData(); 137 } 138 139 public void setLogWriter(PrintWriter out) 140 throws ResourceException { 141 142 } 143 144 public PrintWriter getLogWriter() 145 throws ResourceException { 146 147 return null; 148 } 149 150 protected void close() { 151 ConnectionEvent connEvent = 152 new ConnectionEvent (this, ConnectionEvent.CONNECTION_CLOSED); 153 connEvent.setConnectionHandle(conn); 154 sendConnectionEvent(connEvent); 155 } 156 157 protected void sendConnectionEvent(ConnectionEvent connEvent) { 158 for (int i = listeners.size() - 1; i >= 0; i--) { 159 ConnectionEventListener listener = 160 (ConnectionEventListener ) listeners.get(i); 161 if (connEvent.getId() == ConnectionEvent.CONNECTION_CLOSED) { 162 listener.connectionClosed(connEvent); 163 } else if (connEvent.getId() == 164 ConnectionEvent.CONNECTION_ERROR_OCCURRED) { 165 listener.connectionErrorOccurred(connEvent); 166 } else if (connEvent.getId() == 167 ConnectionEvent.LOCAL_TRANSACTION_STARTED) { 168 listener.localTransactionStarted(connEvent); 169 } else if (connEvent.getId() == 170 ConnectionEvent.LOCAL_TRANSACTION_COMMITTED) { 171 listener.localTransactionCommitted(connEvent); 172 } else if (connEvent.getId() == 173 ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK) { 174 listener.localTransactionRolledback(connEvent); 175 } 176 } 177 } 178 179 public void destroy() 180 throws ResourceException { 181 182 try { 183 cleanupDatabaseHandleCache(roDatabaseHandleCache); 184 cleanupDatabaseHandleCache(rwDatabaseHandleCache); 185 cleanupDatabaseHandleCache(roSecondaryDatabaseHandleCache); 186 cleanupDatabaseHandleCache(rwSecondaryDatabaseHandleCache); 187 env.close(); 188 } catch (DatabaseException DE) { 189 throw new ResourceException (DE.toString()); 190 } 191 } 192 193 public void cleanup() { 194 } 195 196 void removeDatabase(String dbName) 197 throws DatabaseException { 198 199 removeDatabaseFromCache(roDatabaseHandleCache, dbName); 200 removeDatabaseFromCache(rwDatabaseHandleCache, dbName); 201 removeDatabaseFromCache(roSecondaryDatabaseHandleCache, dbName); 202 removeDatabaseFromCache(rwSecondaryDatabaseHandleCache, dbName); 203 env.removeDatabase(null, dbName); 204 } 205 206 long truncateDatabase(String dbName, boolean returnCount) 207 throws DatabaseException { 208 209 removeDatabaseFromCache(roDatabaseHandleCache, dbName); 210 removeDatabaseFromCache(rwDatabaseHandleCache, dbName); 211 removeDatabaseFromCache(roSecondaryDatabaseHandleCache, dbName); 212 removeDatabaseFromCache(rwSecondaryDatabaseHandleCache, dbName); 213 return env.truncateDatabase(null, dbName, returnCount); 214 } 215 216 Database openDatabase(String dbName, DatabaseConfig config) 217 throws DatabaseException { 218 219 if (config.getReadOnly()) { 220 synchronized (roDatabaseHandleCache) { 221 return openDatabaseInternal 222 (roDatabaseHandleCache, dbName, config); 223 } 224 } else { 225 synchronized (rwDatabaseHandleCache) { 226 return openDatabaseInternal 227 (rwDatabaseHandleCache, dbName, config); 228 } 229 } 230 } 231 232 SecondaryDatabase openSecondaryDatabase(String dbName, 233 Database primaryDatabase, 234 SecondaryConfig config) 235 throws DatabaseException { 236 237 if (config.getReadOnly()) { 238 synchronized (roSecondaryDatabaseHandleCache) { 239 return openSecondaryDatabaseInternal 240 (roSecondaryDatabaseHandleCache, dbName, 241 primaryDatabase, config); 242 } 243 } else { 244 synchronized (rwSecondaryDatabaseHandleCache) { 245 return openSecondaryDatabaseInternal 246 (rwSecondaryDatabaseHandleCache, dbName, 247 primaryDatabase, config); 248 } 249 } 250 } 251 252 private Database openDatabaseInternal(Map databaseHandleCache, 253 String dbName, 254 DatabaseConfig config) 255 throws DatabaseException { 256 257 Database db; 258 if (config.getExclusiveCreate()) { 259 db = env.openDatabase(null, dbName, config); 260 databaseHandleCache.put(dbName, db); 261 } else { 262 db = (Database) databaseHandleCache.get(dbName); 263 if (db == null) { 264 db = env.openDatabase(null, dbName, config); 265 databaseHandleCache.put(dbName, db); 266 } else { 267 DbInternal.databaseConfigValidate(config, db.getConfig()); 268 } 269 } 270 return db; 271 } 272 273 private SecondaryDatabase 274 openSecondaryDatabaseInternal(Map databaseHandleCache, 275 String dbName, 276 Database primaryDatabase, 277 SecondaryConfig config) 278 throws DatabaseException { 279 280 SecondaryDatabase db; 281 if (config.getExclusiveCreate()) { 282 db = env.openSecondaryDatabase(null, dbName, 283 primaryDatabase, config); 284 databaseHandleCache.put(dbName, db); 285 } else { 286 db = (SecondaryDatabase) databaseHandleCache.get(dbName); 287 if (db == null) { 288 db = env.openSecondaryDatabase(null, dbName, 289 primaryDatabase, config); 290 databaseHandleCache.put(dbName, db); 291 } else { 292 DbInternal.databaseConfigValidate(config, db.getConfig()); 293 } 294 } 295 return db; 296 } 297 298 private void removeDatabaseFromCache(Map cache, String dbName) 299 throws DatabaseException { 300 301 synchronized (cache) { 302 Database db = (Database) cache.get(dbName); 303 if (db == null) { 304 return; 305 } 306 db.close(); 307 cache.remove(dbName); 308 } 309 } 310 311 private void cleanupDatabaseHandleCache(Map cache) 312 throws DatabaseException { 313 314 synchronized (cache) { 315 Iterator iter = cache.values().iterator(); 316 317 while (iter.hasNext()) { 318 Database db = (Database) iter.next(); 319 db.close(); 320 } 321 } 322 } 323 } 324 | Popular Tags |