KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > support > ResourceHolderSupport


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.transaction.support;
18
19 import java.util.Date JavaDoc;
20
21 import org.springframework.transaction.TransactionTimedOutException;
22
23 /**
24  * Convenient base class for resource holders.
25  *
26  * <p>Features rollback-only support for nested Hibernate transactions.
27  * Can expire after a certain number of seconds or milliseconds,
28  * to determine transactional timeouts.
29  *
30  * @author Juergen Hoeller
31  * @since 02.02.2004
32  * @see org.springframework.jdbc.datasource.DataSourceTransactionManager#doBegin
33  * @see org.springframework.jdbc.datasource.DataSourceUtils#applyTransactionTimeout
34  */

35 public abstract class ResourceHolderSupport {
36
37     private boolean synchronizedWithTransaction = false;
38
39     private boolean rollbackOnly = false;
40
41     private Date JavaDoc deadline;
42
43     private int referenceCount = 0;
44
45
46     /**
47      * Mark the resource as synchronized with a transaction.
48      */

49     public void setSynchronizedWithTransaction(boolean synchronizedWithTransaction) {
50         this.synchronizedWithTransaction = synchronizedWithTransaction;
51     }
52
53     /**
54      * Return whether the resource is synchronized with a transaction.
55      */

56     public boolean isSynchronizedWithTransaction() {
57         return synchronizedWithTransaction;
58     }
59
60     /**
61      * Mark the resource transaction as rollback-only.
62      */

63     public void setRollbackOnly() {
64         this.rollbackOnly = true;
65     }
66
67     /**
68      * Return whether the resource transaction is marked as rollback-only.
69      */

70     public boolean isRollbackOnly() {
71         return rollbackOnly;
72     }
73
74     /**
75      * Set the timeout for this object in seconds.
76      * @param seconds number of seconds until expiration
77      */

78     public void setTimeoutInSeconds(int seconds) {
79         setTimeoutInMillis(seconds * 1000);
80     }
81
82     /**
83      * Set the timeout for this object in milliseconds.
84      * @param millis number of milliseconds until expiration
85      */

86     public void setTimeoutInMillis(long millis) {
87         this.deadline = new Date JavaDoc(System.currentTimeMillis() + millis);
88     }
89
90     /**
91      * Return whether this object has an associated timeout.
92      */

93     public boolean hasTimeout() {
94         return (this.deadline != null);
95     }
96
97     /**
98      * Return the expiration deadline of this object.
99      * @return the deadline as Date object
100      */

101     public Date JavaDoc getDeadline() {
102         return deadline;
103     }
104
105     /**
106      * Return the time to live for this object in seconds.
107      * Rounds up eagerly, e.g. 9.00001 still to 10.
108      * @return number of seconds until expiration
109      * @throws TransactionTimedOutException if the deadline has already been reached
110      */

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     /**
119      * Return the time to live for this object in milliseconds.
120      * @return number of millseconds until expiration
121      * @throws TransactionTimedOutException if the deadline has already been reached
122      */

123     public long getTimeToLiveInMillis() throws TransactionTimedOutException{
124         if (this.deadline == null) {
125             throw new IllegalStateException JavaDoc("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     /**
133      * Set the transaction rollback-only if the deadline has been reached,
134      * and throw a TransactionTimedOutException.
135      */

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     /**
144      * Increase the reference count by one because the holder has been requested
145      * (i.e. someone requested the resource held by it).
146      */

147     public void requested() {
148         this.referenceCount++;
149     }
150
151     /**
152      * Decrease the reference count by one because the holder has been released
153      * (i.e. someone released the resource held by it).
154      */

155     public void released() {
156         this.referenceCount--;
157     }
158
159     /**
160      * Return whether there are still open references to this holder.
161      */

162     public boolean isOpen() {
163         return (this.referenceCount > 0);
164     }
165
166     /**
167      * Clear the transactional state of this resource holder.
168      */

169     public void clear() {
170         this.synchronizedWithTransaction = false;
171         this.rollbackOnly = false;
172         this.deadline = null;
173     }
174
175     /**
176      * Reset this resource holder - transactional state as well as reference count.
177      */

178     public void reset() {
179         clear();
180         this.referenceCount = 0;
181     }
182
183 }
184
Popular Tags