title: How to use JBossTS (Narayana) with Spring Data JPA
body: <p>I am investigating the use of a JTA transaction manager with Spring Data JPA. I have successfully configured Atomikos and Bitronix and am trying to configure JBossTS (Arjuna/Narayana).</p>
  
  <p>I followed the instructions for <a href="https://developer.jboss.org/wiki/JBossTransactionsWithSpring" rel="nofollow noreferrer">configuring JBossTS for Spring</a> and came up with the following configuration:</p>
  
  <pre><code>&lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"&gt;
    &lt;property name="jpaProperties"&gt;
      &lt;props&gt;
        &lt;prop key="hibernate.format_sql"&gt;true"&lt;/prop&gt;
        &lt;prop key="hibernate.hbm2ddl.auto"&gt;create-drop&lt;/prop&gt;
        &lt;prop key="hibernate.id.new_generator_mappings"&gt;false&lt;/prop&gt;
        &lt;prop key="hibernate.transaction.jta.platform"&gt;org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform&lt;/prop&gt;
        &lt;prop key="hibernate.use_sql_comments"&gt;true&lt;/prop&gt;
      &lt;/props&gt;
    &lt;/property&gt;
    &lt;property name="jpaVendorAdapter"&gt;
      &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
        &lt;property name="databasePlatform" value="org.hibernate.dialect.H2Dialect"/&gt;
        &lt;property name="generateDdl" value="true"/&gt;
        &lt;property name="showSql" value="true"/&gt;
      &lt;/bean&gt;
    &lt;/property&gt;
    &lt;property name="jtaDataSource"&gt;
      &lt;bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
        &lt;property name="connectionProperties"&gt;
          &lt;props&gt;
            &lt;prop key="dynamicClass"&gt;com.arjuna.ats.internal.jdbc.drivers.PropertyFileDynamicClass&lt;/prop&gt;
            &lt;prop key="password"&gt;&lt;/prop&gt;
            &lt;prop key="user"&gt;sa&lt;/prop&gt;
          &lt;/props&gt;
        &lt;/property&gt;
        &lt;property name="driverClassName" value="com.arjuna.ats.jdbc.TransactionalDriver"/&gt;
        &lt;property name="url" value="jdbc:arjuna:database.properties"/&gt;
      &lt;/bean&gt;
    &lt;/property&gt;
    &lt;property name="packagesToScan" value="org.example.domain"/&gt;
  &lt;/bean&gt;
  
  &lt;bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager"&gt;
    &lt;property name="transactionManager"&gt;
      &lt;bean class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple"/&gt;
    &lt;/property&gt;
    &lt;property name="userTransaction"&gt;
      &lt;bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction"/&gt;
    &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;transaction:annotation-driven/&gt;
  </code></pre>
  
  <p>However, attempting to run the application throws the following error:</p>
  
  <pre><code>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.&lt;init&gt;(IndirectRecoverableConnection.java:109)
      at com.arjuna.ats.internal.jdbc.ConnectionImple.&lt;init&gt;(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)
  </code></pre>
  
  <p>There does not seem to be any documentation on whether JNDI is mandatory to run JBossTS and if yes, how it can be configured in a standalone application that does not use a JavaEE container.</p>
  
  <p>A sample application <a href="https://github.com/manish-in-java/spring-data-jta" rel="nofollow noreferrer">is available on Github</a> in case the full configuration and source code is required. The problem can be seen by running Maven tests as <code>mvn test -D"spring.profiles.active=jbossts"</code>.</p>