001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.orm.hibernate3; 018 019import java.util.Properties; 020import javax.transaction.Transaction; 021import javax.transaction.TransactionManager; 022 023import org.hibernate.transaction.TransactionManagerLookup; 024 025/** 026 * Implementation of Hibernate's {@link TransactionManagerLookup} interface 027 * that returns a Spring-managed JTA {@link TransactionManager}, determined 028 * by LocalSessionFactoryBean's "jtaTransactionManager" property. 029 * 030 * <p>The main advantage of this TransactionManagerLookup is that it avoids 031 * double configuration of JTA specifics. A single TransactionManager bean can 032 * be used for both JtaTransactionManager and LocalSessionFactoryBean, with no 033 * JTA setup in Hibernate configuration. 034 * 035 * <p>Alternatively, use Hibernate's own TransactionManagerLookup implementations: 036 * Spring's JtaTransactionManager only requires a TransactionManager for suspending 037 * and resuming transactions, so you might not need to apply such special Spring 038 * configuration at all. 039 * 040 * @author Juergen Hoeller 041 * @since 1.2 042 * @see LocalSessionFactoryBean#setJtaTransactionManager 043 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager 044 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x 045 */ 046@Deprecated 047public class LocalTransactionManagerLookup implements TransactionManagerLookup { 048 049 private final TransactionManager transactionManager; 050 051 052 public LocalTransactionManagerLookup() { 053 TransactionManager tm = LocalSessionFactoryBean.getConfigTimeTransactionManager(); 054 // absolutely needs thread-bound TransactionManager to initialize 055 if (tm == null) { 056 throw new IllegalStateException("No JTA TransactionManager found - " + 057 "'jtaTransactionManager' property must be set on LocalSessionFactoryBean"); 058 } 059 this.transactionManager = tm; 060 } 061 062 @Override 063 public TransactionManager getTransactionManager(Properties props) { 064 return this.transactionManager; 065 } 066 067 @Override 068 public String getUserTransactionName() { 069 return null; 070 } 071 072 @Override 073 public Object getTransactionIdentifier(Transaction transaction) { 074 return transaction; 075 } 076 077}