1 16 17 package org.springframework.transaction.support; 18 19 import java.util.Date ; 20 21 import org.springframework.transaction.TransactionTimedOutException; 22 23 35 public abstract class ResourceHolderSupport { 36 37 private boolean synchronizedWithTransaction = false; 38 39 private boolean rollbackOnly = false; 40 41 private Date deadline; 42 43 private int referenceCount = 0; 44 45 46 49 public void setSynchronizedWithTransaction(boolean synchronizedWithTransaction) { 50 this.synchronizedWithTransaction = synchronizedWithTransaction; 51 } 52 53 56 public boolean isSynchronizedWithTransaction() { 57 return synchronizedWithTransaction; 58 } 59 60 63 public void setRollbackOnly() { 64 this.rollbackOnly = true; 65 } 66 67 70 public boolean isRollbackOnly() { 71 return rollbackOnly; 72 } 73 74 78 public void setTimeoutInSeconds(int seconds) { 79 setTimeoutInMillis(seconds * 1000); 80 } 81 82 86 public void setTimeoutInMillis(long millis) { 87 this.deadline = new Date (System.currentTimeMillis() + millis); 88 } 89 90 93 public boolean hasTimeout() { 94 return (this.deadline != null); 95 } 96 97 101 public Date getDeadline() { 102 return deadline; 103 } 104 105 111 public int getTimeToLiveInSeconds() { 112 double diff = ((double) getTimeToLiveInMillis()) / 1000; 113 int secs = (int) Math.ceil(diff); 114 checkTransactionTimeout(secs <= 0); 115 return secs; 116 } 117 118 123 public long getTimeToLiveInMillis() throws TransactionTimedOutException{ 124 if (this.deadline == null) { 125 throw new IllegalStateException ("No timeout specified for this resource holder"); 126 } 127 long timeToLive = this.deadline.getTime() - System.currentTimeMillis(); 128 checkTransactionTimeout(timeToLive <= 0); 129 return timeToLive; 130 } 131 132 136 private void checkTransactionTimeout(boolean deadlineReached) throws TransactionTimedOutException { 137 if (deadlineReached) { 138 setRollbackOnly(); 139 throw new TransactionTimedOutException("Transaction timed out: deadline was " + this.deadline); 140 } 141 } 142 143 147 public void requested() { 148 this.referenceCount++; 149 } 150 151 155 public void released() { 156 this.referenceCount--; 157 } 158 159 162 public boolean isOpen() { 163 return (this.referenceCount > 0); 164 } 165 166 169 public void clear() { 170 this.synchronizedWithTransaction = false; 171 this.rollbackOnly = false; 172 this.deadline = null; 173 } 174 175 178 public void reset() { 179 clear(); 180 this.referenceCount = 0; 181 } 182 183 } 184 | Popular Tags |