1 17 18 21 package org.quartz.impl.jdbcjobstore; 22 23 import java.sql.Connection ; 24 import java.sql.SQLException ; 25 26 import org.quartz.JobPersistenceException; 27 import org.quartz.SchedulerConfigException; 28 import org.quartz.spi.ClassLoadHelper; 29 import org.quartz.spi.SchedulerSignaler; 30 import org.quartz.utils.DBConnectionManager; 31 32 50 public class JobStoreCMT extends JobStoreSupport { 51 52 59 60 protected String nonManagedTxDsName; 61 62 protected boolean dontSetNonManagedTXConnectionAutoCommitFalse = false; 64 65 66 protected boolean setTxIsolationLevelReadCommitted = false; 67 68 75 76 82 public void setNonManagedTXDataSource(String nonManagedTxDsName) { 83 this.nonManagedTxDsName = nonManagedTxDsName; 84 } 85 86 92 public String getNonManagedTXDataSource() { 93 return nonManagedTxDsName; 94 } 95 96 public boolean isDontSetNonManagedTXConnectionAutoCommitFalse() { 97 return dontSetNonManagedTXConnectionAutoCommitFalse; 98 } 99 100 107 public void setDontSetNonManagedTXConnectionAutoCommitFalse(boolean b) { 108 dontSetNonManagedTXConnectionAutoCommitFalse = b; 109 } 110 111 112 public boolean isTxIsolationLevelReadCommitted() { 113 return setTxIsolationLevelReadCommitted; 114 } 115 116 121 public void setTxIsolationLevelReadCommitted(boolean b) { 122 setTxIsolationLevelReadCommitted = b; 123 } 124 125 126 public void initialize(ClassLoadHelper loadHelper, 127 SchedulerSignaler signaler) throws SchedulerConfigException { 128 129 if (nonManagedTxDsName == null) { 130 throw new SchedulerConfigException( 131 "Non-ManagedTX DataSource name not set! " + 132 "If your 'org.quartz.jobStore.dataSource' is XA, then set " + 133 "'org.quartz.jobStore.nonManagedTXDataSource' to a non-XA "+ 134 "datasource (for the same DB). " + 135 "Otherwise, you can set them to be the same."); 136 } 137 138 if (getLockHandler() == null) { 139 setUseDBLocks(true); 142 } 143 144 super.initialize(loadHelper, signaler); 145 146 getLog().info("JobStoreCMT initialized."); 147 } 148 149 public void shutdown() { 150 151 super.shutdown(); 152 153 try { 154 DBConnectionManager.getInstance().shutdown(getNonManagedTXDataSource()); 155 } catch (SQLException sqle) { 156 getLog().warn("Database connection shutdown unsuccessful.", sqle); 157 } 158 } 159 160 protected Connection getNonManagedTXConnection() 161 throws JobPersistenceException { 162 Connection conn = null; 163 try { 164 conn = DBConnectionManager.getInstance().getConnection( 165 getNonManagedTXDataSource()); 166 } catch (SQLException sqle) { 167 throw new JobPersistenceException( 168 "Failed to obtain DB connection from data source '" 169 + getNonManagedTXDataSource() + "': " 170 + sqle.toString(), sqle); 171 } catch (Throwable e) { 172 throw new JobPersistenceException( 173 "Failed to obtain DB connection from data source '" 174 + getNonManagedTXDataSource() + "': " 175 + e.toString(), e, 176 JobPersistenceException.ERR_PERSISTENCE_CRITICAL_FAILURE); 177 } 178 179 if (conn == null) { 180 throw new JobPersistenceException( 181 "Could not get connection from DataSource '" 182 + getNonManagedTXDataSource() + "'"); 183 } 184 185 conn = getAttributeRestoringConnection(conn); 187 188 try { 190 if (!isDontSetNonManagedTXConnectionAutoCommitFalse()) { 191 conn.setAutoCommit(false); 192 } 193 194 if (isTxIsolationLevelReadCommitted()) { 195 conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 196 } 197 } catch (SQLException sqle) { 198 getLog().warn("Failed to override connection auto commit/transaction isolation.", sqle); 199 } catch (Throwable e) { 200 try { conn.close(); } catch(Throwable tt) {} 201 202 throw new JobPersistenceException( 203 "Failure setting up connection.", e); 204 } 205 206 return conn; 207 } 208 209 224 protected Object executeInLock( 225 String lockName, 226 TransactionCallback txCallback) throws JobPersistenceException { 227 boolean transOwner = false; 228 Connection conn = null; 229 try { 230 if (lockName != null) { 231 if (getLockHandler().requiresConnection()) { 234 conn = getConnection(); 235 } 236 237 transOwner = getLockHandler().obtainLock(conn, lockName); 238 } 239 240 if (conn == null) { 241 conn = getConnection(); 242 } 243 244 return txCallback.execute(conn); 245 } finally { 246 try { 247 releaseLock(conn, LOCK_TRIGGER_ACCESS, transOwner); 248 } finally { 249 cleanupConnection(conn); 250 } 251 } 252 } 253 } 254 255 | Popular Tags |