如何将 JBossTS (Narayana) 与 Spring Data JPA 一起使用

How to use JBossTS (Narayana) with Spring Data JPA

我正在调查 JTA 事务管理器与 Spring Data JPA 的使用。我已成功配置 Atomikos 和 Bitronix,并且正在尝试配置 JBossTS (Arjuna/Narayana)。

我按照 configuring JBossTS for Spring 的说明进行了操作,得出了以下配置:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
  <property name="jpaProperties">
    <props>
      <prop key="hibernate.format_sql">true"</prop>
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
      <prop key="hibernate.id.new_generator_mappings">false</prop>
      <prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform</prop>
      <prop key="hibernate.use_sql_comments">true</prop>
    </props>
  </property>
  <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect"/>
      <property name="generateDdl" value="true"/>
      <property name="showSql" value="true"/>
    </bean>
  </property>
  <property name="jtaDataSource">
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="connectionProperties">
        <props>
          <prop key="dynamicClass">com.arjuna.ats.internal.jdbc.drivers.PropertyFileDynamicClass</prop>
          <prop key="password"></prop>
          <prop key="user">sa</prop>
        </props>
      </property>
      <property name="driverClassName" value="com.arjuna.ats.jdbc.TransactionalDriver"/>
      <property name="url" value="jdbc:arjuna:database.properties"/>
    </bean>
  </property>
  <property name="packagesToScan" value="org.example.domain"/>
</bean>

<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
  <property name="transactionManager">
    <bean class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple"/>
  </property>
  <property name="userTransaction">
    <bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction"/>
  </property>
</bean>

<transaction:annotation-driven/>

但是,尝试 运行 应用程序会引发以下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
Caused by: org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
Caused by: java.sql.SQLException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at com.arjuna.ats.internal.jdbc.IndirectRecoverableConnection.createDataSource(IndirectRecoverableConnection.java:361)
    at com.arjuna.ats.internal.jdbc.IndirectRecoverableConnection.<init>(IndirectRecoverableConnection.java:109)
    at com.arjuna.ats.internal.jdbc.ConnectionImple.<init>(ConnectionImple.java:107)
    at com.arjuna.ats.internal.jdbc.ConnectionManager.create(ConnectionManager.java:110)
    at com.arjuna.ats.jdbc.TransactionalDriver.connect(TransactionalDriver.java:87)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:196)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:159)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)
    at org.hibernate.resource.transaction.backend.jta.internal.DdlTransactionIsolatorJtaImpl.prepare(DdlTransactionIsolatorJtaImpl.java:49)
    ... 60 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at com.arjuna.ats.internal.jdbc.IndirectRecoverableConnection.createDataSource(IndirectRecoverableConnection.java:346)

似乎没有任何文档说明 JNDI 是否对 运行 JBossTS 是强制性的,如果是,如何在不使用 JavaEE 容器的独立应用程序中配置它。

示例应用程序 is available on Github 以防需要完整配置和源代码。通过 运行ning Maven 测试可以看出该问题为 mvn test -D"spring.profiles.active=jbossts".

  1. <prop key="dynamicClass"> 替换为 <prop key="DYNAMIC_CLASS">
  2. 使用 target/classes
  3. 指定属性文件
  4. 使用H2文件数据库代替内存

更多信息可以查看narayana.io