1 8 9 package com.sleepycat.je.txn; 10 11 import com.sleepycat.je.Database; 12 import com.sleepycat.je.DatabaseException; 13 import com.sleepycat.je.DbInternal; 14 import com.sleepycat.je.Environment; 15 import com.sleepycat.je.Transaction; 16 import com.sleepycat.je.TransactionConfig; 17 import com.sleepycat.je.dbi.DatabaseImpl; 18 import com.sleepycat.je.dbi.EnvironmentImpl; 19 20 23 public class LockerFactory { 24 25 29 public static Locker getWritableLocker(Environment env, 30 Transaction userTxn, 31 boolean dbIsTransactional) 32 throws DatabaseException { 33 34 return getWritableLocker(env, userTxn, dbIsTransactional, false, null); 35 } 36 37 45 public static Locker getWritableLocker(Environment env, 46 Transaction userTxn, 47 boolean dbIsTransactional, 48 boolean retainNonTxnLocks, 49 TransactionConfig autoCommitConfig) 50 throws DatabaseException { 51 52 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 53 boolean envIsTransactional = envImpl.isTransactional(); 54 55 if (userTxn == null) { 56 Transaction xaLocker = env.getThreadTransaction(); 57 if (xaLocker != null) { 58 return DbInternal.getLocker(xaLocker); 59 } 60 } 61 62 if (dbIsTransactional && userTxn == null) { 63 64 if (autoCommitConfig == null) { 65 autoCommitConfig = DbInternal.getDefaultTxnConfig(env); 66 } 67 return new AutoTxn(envImpl, autoCommitConfig); 68 69 } else if (userTxn == null) { 70 71 if (retainNonTxnLocks) { 72 return new BasicLocker(envImpl); 73 } else { 74 return new ThreadLocker(envImpl); 75 } 76 77 } else { 78 79 83 if (!envIsTransactional) { 84 throw new DatabaseException 85 ("A Transaction cannot be used because the"+ 86 " environment was opened" + 87 " non-transactionally"); 88 } 89 if (!dbIsTransactional) { 90 throw new DatabaseException 91 ("A Transaction cannot be used because the" + 92 " database was opened" + 93 " non-transactionally"); 94 } 95 96 103 Locker locker = DbInternal.getLocker(userTxn); 104 if (locker.isReadCommittedIsolation() && !retainNonTxnLocks) { 105 return new ReadCommittedLocker(envImpl, locker); 106 } else { 107 return locker; 108 } 109 } 110 } 111 112 116 public static Locker getReadableLocker(Environment env, 117 Transaction userTxn, 118 boolean dbIsTransactional, 119 boolean retainNonTxnLocks, 120 boolean readCommittedIsolation) 121 throws DatabaseException { 122 123 if (userTxn != null && !dbIsTransactional) { 124 throw new DatabaseException 125 ("A Transaction cannot be used because the" + 126 " database was opened" + 127 " non-transactionally"); 128 } 129 130 Locker locker = null; 131 if (userTxn != null) { 132 133 139 locker = DbInternal.getLocker(userTxn); 140 if (locker.isReadCommittedIsolation()) { 141 readCommittedIsolation = true; 142 } 143 } 144 145 return getReadableLocker 146 (env, locker, retainNonTxnLocks, readCommittedIsolation); 147 } 148 149 153 public static Locker getReadableLocker(Environment env, 154 Database dbHandle, 155 Locker locker, 156 boolean retainNonTxnLocks, 157 boolean readCommittedIsolation) 158 throws DatabaseException { 159 160 DatabaseImpl dbImpl = DbInternal.dbGetDatabaseImpl(dbHandle); 161 if (!dbImpl.isTransactional() && 162 locker != null && 163 locker.isTransactional()) { 164 throw new DatabaseException 165 ("A Transaction cannot be used because the" + 166 " database was opened" + 167 " non-transactionally"); 168 } 169 170 174 if (locker != null && 175 !locker.isTransactional() && 176 !retainNonTxnLocks) { 177 locker = null; 178 } 179 180 185 if (locker != null && locker.isReadCommittedIsolation()) { 186 readCommittedIsolation = true; 187 } 188 189 return getReadableLocker 190 (env, locker, retainNonTxnLocks, readCommittedIsolation); 191 } 192 193 197 private static Locker getReadableLocker(Environment env, 198 Locker locker, 199 boolean retainNonTxnLocks, 200 boolean readCommittedIsolation) 201 throws DatabaseException { 202 203 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 204 205 if (locker == null) { 206 Transaction xaTxn = env.getThreadTransaction(); 207 if (xaTxn != null) { 208 return DbInternal.getLocker(xaTxn); 209 } 210 } 211 212 if (locker == null) { 213 214 222 if (retainNonTxnLocks) { 223 locker = new BasicLocker(envImpl); 224 } else { 225 locker = new ThreadLocker(envImpl); 226 } 227 } else { 228 229 236 if (readCommittedIsolation && !retainNonTxnLocks) { 237 locker = new ReadCommittedLocker(envImpl, locker); 238 } 239 } 240 return locker; 241 } 242 } 243 | Popular Tags |