KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate > LocalTransactionManagerLookup


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.orm.hibernate;
18
19 import java.util.Properties JavaDoc;
20
21 import javax.transaction.TransactionManager JavaDoc;
22
23 import net.sf.hibernate.transaction.TransactionManagerLookup;
24
25 /**
26  * Implementation of Hibernate's TransactionManagerLookup interface that
27  * returns a Spring-managed JTA TransactionManager, determined by
28  * LocalSessionFactoryBean's "transactionManager" property.
29  *
30  * <p>The main advantage of this TransactionManagerLookup is that it avoids
31  * double configuration of JTA specifics. A single TransactionManager bean can
32  * be used for both JtaTransactionManager and LocalSessionFactoryBean, with no
33  * JTA setup in Hibernate configuration.
34  *
35  * <p>Alternatively, use Hibernate's own TransactionManagerLookup implementations:
36  * Spring's JtaTransactionManager only requires a TransactionManager for suspending
37  * and resuming transactions, so you might not need to apply such special Spring
38  * configuration at all.
39  *
40  * @author Juergen Hoeller
41  * @since 21.01.2004
42  * @see LocalSessionFactoryBean#setJtaTransactionManager
43  * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
44  */

45 public class LocalTransactionManagerLookup implements TransactionManagerLookup {
46
47     private final TransactionManager JavaDoc transactionManager;
48
49
50     public LocalTransactionManagerLookup() {
51         TransactionManager JavaDoc tm = LocalSessionFactoryBean.getConfigTimeTransactionManager();
52         // absolutely needs thread-bound DataSource to initialize
53
if (tm == null) {
54             throw new IllegalStateException JavaDoc("No JTA TransactionManager found - " +
55                 "jtaTransactionManager property must be set on LocalSessionFactoryBean");
56         }
57         this.transactionManager = tm;
58     }
59
60     public TransactionManager JavaDoc getTransactionManager(Properties JavaDoc props) {
61         return transactionManager;
62     }
63
64     public String JavaDoc getUserTransactionName() {
65         return null;
66     }
67
68 }
69
Popular Tags