1 2 18 19 22 package org.quartz.ee.jta; 23 24 import javax.transaction.Status ; 25 import javax.transaction.SystemException ; 26 import javax.transaction.UserTransaction ; 27 28 import org.quartz.Scheduler; 29 import org.quartz.SchedulerException; 30 import org.quartz.core.JobRunShell; 31 import org.quartz.core.JobRunShellFactory; 32 import org.quartz.core.SchedulingContext; 33 34 45 public class JTAJobRunShell extends JobRunShell { 46 47 54 55 private UserTransaction ut; 56 57 64 65 70 public JTAJobRunShell(JobRunShellFactory jobRunShellFactory, 71 Scheduler scheduler, SchedulingContext schdCtxt) { 72 super(jobRunShellFactory, scheduler, schdCtxt); 73 } 74 75 82 83 protected void begin() throws SchedulerException { 84 cleanupUserTransaction(); 89 90 boolean beganSuccessfully = false; 91 try { 92 getLog().debug("Looking up UserTransaction."); 93 ut = UserTransactionHelper.lookupUserTransaction(); 94 95 getLog().debug("Beginning UserTransaction."); 96 ut.begin(); 97 98 beganSuccessfully = true; 99 } catch (SchedulerException se) { 100 throw se; 101 } catch (Exception nse) { 102 103 throw new SchedulerException( 104 "JTAJobRunShell could not start UserTransaction.", nse); 105 } finally { 106 if (beganSuccessfully == false) { 107 cleanupUserTransaction(); 108 } 109 } 110 } 111 112 protected void complete(boolean successfulExecution) 113 throws SchedulerException { 114 if (ut == null) { 115 return; 116 } 117 118 try { 119 try { 120 if (ut.getStatus() == Status.STATUS_MARKED_ROLLBACK) { 121 getLog().debug("UserTransaction marked for rollback only."); 122 successfulExecution = false; 123 } 124 } catch (SystemException e) { 125 throw new SchedulerException( 126 "JTAJobRunShell could not read UserTransaction status.", e); 127 } 128 129 if (successfulExecution) { 130 try { 131 getLog().debug("Committing UserTransaction."); 132 ut.commit(); 133 } catch (Exception nse) { 134 throw new SchedulerException( 135 "JTAJobRunShell could not commit UserTransaction.", nse); 136 } 137 } else { 138 try { 139 getLog().debug("Rolling-back UserTransaction."); 140 ut.rollback(); 141 } catch (Exception nse) { 142 throw new SchedulerException( 143 "JTAJobRunShell could not rollback UserTransaction.", 144 nse); 145 } 146 } 147 } finally { 148 cleanupUserTransaction(); 149 } 150 } 151 152 155 public void passivate() { 156 cleanupUserTransaction(); 157 super.passivate(); 158 } 159 160 private void cleanupUserTransaction() { 161 if (ut != null) { 162 UserTransactionHelper.returnUserTransaction(ut); 163 ut = null; 164 } 165 } 166 } | Popular Tags |