01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.hibernate3;
18:
19: import java.util.Properties;
20:
21: import javax.transaction.TransactionManager;
22:
23: import org.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 1.2
42: * @see LocalSessionFactoryBean#setJtaTransactionManager
43: * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
44: */
45: public class LocalTransactionManagerLookup implements
46: TransactionManagerLookup {
47:
48: private final TransactionManager transactionManager;
49:
50: public LocalTransactionManagerLookup() {
51: TransactionManager tm = LocalSessionFactoryBean
52: .getConfigTimeTransactionManager();
53: // absolutely needs thread-bound DataSource to initialize
54: if (tm == null) {
55: throw new IllegalStateException(
56: "No JTA TransactionManager found - "
57: + "jtaTransactionManager property must be set on LocalSessionFactoryBean");
58: }
59: this .transactionManager = tm;
60: }
61:
62: public TransactionManager getTransactionManager(Properties props) {
63: return transactionManager;
64: }
65:
66: public String getUserTransactionName() {
67: return null;
68: }
69:
70: }
|