KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > transaction > TransactionComponent


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.transaction;
18
19 import javax.transaction.UserTransaction JavaDoc;
20
21 import org.alfresco.service.transaction.TransactionService;
22 import org.alfresco.util.transaction.SpringAwareUserTransaction;
23 import org.springframework.transaction.PlatformTransactionManager;
24 import org.springframework.transaction.TransactionDefinition;
25
26 /**
27  * Default implementation of Transaction Service
28  *
29  * @author David Caruana
30  */

31 public class TransactionComponent implements TransactionService
32 {
33     private PlatformTransactionManager transactionManager;
34     private boolean readOnly = false;
35     
36     /**
37      * Set the transaction manager to use
38      *
39      * @param transactionManager platform transaction manager
40      */

41     public void setTransactionManager(PlatformTransactionManager transactionManager)
42     {
43         this.transactionManager = transactionManager;
44     }
45
46     /**
47      * Set the read-only mode for all generated transactions.
48      *
49      * @param allowWrite false if all transactions must be read-only
50      */

51     public void setAllowWrite(boolean allowWrite)
52     {
53         this.readOnly = !allowWrite;
54     }
55     
56     public boolean isReadOnly()
57     {
58         return readOnly;
59     }
60
61     /**
62      * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED
63      */

64     public UserTransaction JavaDoc getUserTransaction()
65     {
66         SpringAwareUserTransaction txn = new SpringAwareUserTransaction(
67                 transactionManager,
68                 this.readOnly,
69                 TransactionDefinition.ISOLATION_DEFAULT,
70                 TransactionDefinition.PROPAGATION_REQUIRED,
71                 TransactionDefinition.TIMEOUT_DEFAULT);
72         return txn;
73     }
74     
75     /**
76      * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED
77      */

78     public UserTransaction JavaDoc getUserTransaction(boolean readOnly)
79     {
80         SpringAwareUserTransaction txn = new SpringAwareUserTransaction(
81                 transactionManager,
82                 (readOnly | this.readOnly),
83                 TransactionDefinition.ISOLATION_DEFAULT,
84                 TransactionDefinition.PROPAGATION_REQUIRED,
85                 TransactionDefinition.TIMEOUT_DEFAULT);
86         return txn;
87     }
88
89     /**
90      * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRES_NEW
91      */

92     public UserTransaction JavaDoc getNonPropagatingUserTransaction()
93     {
94         SpringAwareUserTransaction txn = new SpringAwareUserTransaction(
95                 transactionManager,
96                 this.readOnly,
97                 TransactionDefinition.ISOLATION_DEFAULT,
98                 TransactionDefinition.PROPAGATION_REQUIRES_NEW,
99                 TransactionDefinition.TIMEOUT_DEFAULT);
100         return txn;
101     }
102
103     /**
104      * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRES_NEW
105      */

106     public UserTransaction JavaDoc getNonPropagatingUserTransaction(boolean readOnly)
107     {
108         SpringAwareUserTransaction txn = new SpringAwareUserTransaction(
109                 transactionManager,
110                 (readOnly | this.readOnly),
111                 TransactionDefinition.ISOLATION_DEFAULT,
112                 TransactionDefinition.PROPAGATION_REQUIRES_NEW,
113                 TransactionDefinition.TIMEOUT_DEFAULT);
114         return txn;
115     }
116 }
117
Popular Tags