1. /*
  2. * Copyright 2002-2004 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. package org.springframework.orm.hibernate;
  17. import java.util.Properties;
  18. import javax.transaction.TransactionManager;
  19. import net.sf.hibernate.transaction.TransactionManagerLookup;
  20. /**
  21. * Implementation of Hibernate's TransactionManagerLookup interface that
  22. * returns a Spring-managed JTA TransactionManager, determined by
  23. * LocalSessionFactoryBean's "transactionManager" property.
  24. *
  25. * <p>The main advantage of this TransactionManagerLookup is that it avoids
  26. * double configuration of JTA specifics. A single TransactionManager bean can
  27. * be used for both JtaTransactionManager and LocalSessionFactoryBean, with no
  28. * JTA setup in Hibernate configuration.
  29. *
  30. * <p>Alternatively, use Hibernate's own TransactionManagerLookup implementations:
  31. * Spring's JtaTransactionManager only requires a TransactionManager for suspending
  32. * and resuming transactions, so you might not need to apply such special Spring
  33. * configuration at all.
  34. *
  35. * @author Juergen Hoeller
  36. * @since 21.01.2004
  37. * @see LocalSessionFactoryBean#setJtaTransactionManager
  38. * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
  39. */
  40. public class LocalTransactionManagerLookup implements TransactionManagerLookup {
  41. /**
  42. * This will hold the TransactionManager to use for the currently configured
  43. * Hibernate SessionFactory. It will be set just before initialization
  44. * of the respective SessionFactory, and reset immediately afterwards.
  45. */
  46. protected static ThreadLocal configTimeTransactionManagerHolder = new ThreadLocal();
  47. private final TransactionManager transactionManager;
  48. public LocalTransactionManagerLookup() {
  49. TransactionManager tm = (TransactionManager) configTimeTransactionManagerHolder.get();
  50. // absolutely needs thread-bound DataSource to initialize
  51. if (tm == null) {
  52. throw new IllegalStateException("No TransactionManager found - transactionManager property must be set on LocalSessionFactoryBean");
  53. }
  54. this.transactionManager = tm;
  55. }
  56. public TransactionManager getTransactionManager(Properties props) {
  57. return transactionManager;
  58. }
  59. public String getUserTransactionName() {
  60. return null;
  61. }
  62. }