如何在Spring Data JPA中使用JBossTS(Narayana)

时间:2016-09-24 06:13:40

标签: spring-data-jpa jta jbossts

我正在调查使用Spring Data JPA的JTA事务管理器。我已经成功配置了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,以防需要完整配置和源代码。将Maven测试作为mvn test -D"spring.profiles.active=jbossts"运行可以看出问题。

1 个答案:

答案 0 :(得分:1)

  1. <prop key="dynamicClass">替换为<prop key="DYNAMIC_CLASS">
  2. 使用target/classes
  3. 指定属性文件
  4. 使用H2文件数据库而不是内存
  5. 有关详细信息,请参阅narayana.io