View Javadoc
1   /*
2    *  Copyright 2013 Chris Pheby
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.jadira.usertype.spi.jta;
17  
18  import javax.persistence.EntityManagerFactory;
19  import javax.persistence.PersistenceException;
20  
21  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
22  import org.springframework.transaction.jta.JtaTransactionManager;
23  
24  /**
25   * This class provides an extension of Spring's {@link LocalContainerEntityManagerFactoryBean} which provides a mechanism for associating a PlatformTransactionManager with Hibernate's EntityManager.
26   * 
27   * To use this class set the Hibernate / JTA property 'hibernate.transaction.jta.platform' to org.jadira.usertype.spi.jta.LocalTransactionManagerPlatform. Then configure the chosen transaction manager
28   * via the {@link #setTransactionManager(JtaTransactionManager)} method.
29   * 
30   * If you are using WebSphere be sure to use the {@link SpringWebSphereUowTransactionManager} as the standard Spring provided implementation will not work with this class.
31   */
32  public class HibernateEntityManagerFactoryBean extends LocalContainerEntityManagerFactoryBean {
33  
34      private JtaTransactionManager transactionManager;
35  
36      private static final ThreadLocal<JtaTransactionManager> configurationTransactionManagerHolder = new ThreadLocal<JtaTransactionManager>();
37  
38      static JtaTransactionManager getConfigurationTransactionManager() {
39          return configurationTransactionManagerHolder.get();
40      }
41  
42      @Override
43      protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException {
44  
45          if (this.transactionManager != null) {
46              configurationTransactionManagerHolder.set(this.transactionManager);
47          }
48  
49          try {
50              return super.createNativeEntityManagerFactory();
51          } finally {
52  
53              if (this.transactionManager != null) {
54                  configurationTransactionManagerHolder.set(null);
55              }
56          }
57      }
58  
59      /**
60       * Associate a transaction manager with this session
61       * @param transactionManager The {@link JtaTransactionManager}
62       */
63      public void setTransactionManager(JtaTransactionManager transactionManager) {
64          this.transactionManager = transactionManager;
65      }
66  }