谁有Spring + Hibernate + 2LC Infinispan + JPA的工作示例?

时间:2012-01-30 14:43:35

标签: hibernate spring jpa spring-transactions infinispan

我整个上午都在敲打我的脑袋,以取代Infinispan的EHCache。我尝试了很多不同版本的配置。但似乎总是在Hibernate和Spring之间关于事务管理的缺失链接。为了我自己的耻辱,我不得不承认我似乎无法找到让这项工作一起工作的方法。

我正在使用由JPA和Hibernate(4.0.1)支持的Spring(3.1)的经典解决方案。随着EHCache成为第二级和查询缓存。全部部署在独立的Tomcat 7上。

有人在这里愿意为此分享一个有效的Spring配置吗?

非常感谢!

我包括Spring配置 - 如果某个地方存在明显缺陷......

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

          http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

  <!-- Scan our code for Spring annotations @Component, @Repository and @Service annotated classes are added -->
  <!-- to the Spring configuration as if they're declared in this file. -->
  <context:component-scan base-package="com.foo" scoped-proxy="targetClass"/>


  <!-- configuration transactions are done with @Transactional -->
  <tx:annotation-driven transaction-manager="transactionManager"/>

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>

  <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>

  <!-- Describes how to connect to the database -->
  <!-- Describes to reuse database connections as making them is expensive -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="user" value="${database.user.name}"/>
    <property name="password" value="${database.user.password}"/>
    <property name="driverClass" value="${database.driver}"/>
    <property name="jdbcUrl" value="${database.url}"/>
    <property name="initialPoolSize" value="1"/>
    <property name="maxPoolSize" value="4"/>
    <property name="minPoolSize" value="1"/>
    <property name="acquireIncrement" value="1"/>
    <property name="acquireRetryAttempts" value="0"/>
  </bean>

  <!-- Describes how to create hibernate sessions -->
  <!-- "persistenceUnitName" points to META-INF/persistence.xml -->
  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
    <property name="persistenceUnitName" value="foo"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaProperties">
      <props>
        <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.InfinispanRegionFactory</prop>
        <prop key="hibernate.cache.use_second_level_cache">true</prop>
        <prop key="hibernate.cache.use_query_cache">true</prop>
        <prop key="hibernate.cache.infinispan.statistics">true</prop>
        <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
        <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossStandaloneJTAManagerLookup</prop>
        <prop key="hibernate.hbm2ddl.auto">create</prop>
        <prop key="hibernate.jdbc.batch_size">1000</prop>
        <prop key="hibernate.show_sql">false</prop>
        <prop key="hibernate.dialect">${database.hibernate.dialect}</prop>
      </props>
    </property>
  </bean>
</beans>

对事务查找类使用org.hibernate.transaction.JBossTSStandaloneTransactionManagerLookup时出现异常。

Caused by: org.hibernate.cache.CacheException: Unable to start region factory
    at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:289)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:271)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
    ... 73 more
Caused by: org.hibernate.service.jta.platform.spi.JtaPlatformException: Could not obtain JBoss Transactions transaction manager instance
    at org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform.locateTransactionManager(JBossStandAloneJtaPlatform.java:52)
    at org.hibernate.service.jta.platform.internal.AbstractJtaPlatform.retrieveTransactionManager(AbstractJtaPlatform.java:88)
    at org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup.getTransactionManager(HibernateTransactionManagerLookup.java:48)
    at org.hibernate.cache.infinispan.InfinispanRegionFactory.start(InfinispanRegionFactory.java:274)
    ... 77 more
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [com.arjuna.ats.jta.TransactionManager]
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141)
    at org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform.locateTransactionManager(JBossStandAloneJtaPlatform.java:46)
    ... 80 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.arjuna.ats.jta.TransactionManager
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
    ... 81 more

添加依赖项后出现异常(随之而来的是很多间接依赖项)

Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.ejb.Ejb3Configuration
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:71)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 67 more

1 个答案:

答案 0 :(得分:2)

我有类似的要求在Tomcat中运行jpa + l2 cache + infinispan。

以下的hibernate配置对我有用。

hibernate.cache.region.factory_class - &gt; org.hibernate.cache.infinispan.InfinispanRegionFactory hibernate.transaction.jta.platform - &gt; org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform hibernate.transaction.manager_lookup_class - &gt; org.hibernate.cache.infinispan.tm.HibernateTransactionManagerLookup

我有以下额外的依赖项。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-infinispan</artifactId>
    <version>4.2.5.Final</version>
 </dependency>

<dependency>
    <groupId>org.jboss.jbossts</groupId>
    <artifactId>jbossjta</artifactId>
    <version>4.16.4.Final</version>
</dependency>