H2 Database / Bitronix没有Java EE

时间:2013-09-30 11:49:47

标签: java jpa jndi jta bitronix

对于功能的简单测试(遵循教程),我尝试使用Bitronix和H2内存数据库设置原始JTA环境。 该测试应该在一个简单的Java应用程序(没有Java EE,没有服务器等)

中作为单元测试运行

不幸的是我没有让它运行,这是我的设置:

  1. 我在教程后添加了一些罐子(通过maven):

    • btm,h2,hibernate-entitymanager和hibernate-jpa-2.0-api
  2. 我将persistence.xml,hibernate.cfg.xml和jndi.properties文件添加到了类路径中。

  3. 在测试方法中(脏...)我把这段代码:

    PoolingDataSource ds = new PoolingDataSource();
    ds.setUniqueName( "jdbc/BitronixJTADataSource" );
    ds.setClassName( "org.h2.jdbcx.JdbcDataSource" );
    ds.setMaxPoolSize( 3 );
    ds.setAllowLocalTransactions( true );
    ds.getDriverProperties().put( "user", "sa" );
    ds.getDriverProperties().put( "password", "sasa" );
    ds.getDriverProperties().put( "URL", "jdbc:h2:mem:mydb" );
    ds.init();
    
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.persistence.jpa");
    
  4. 这是我的persistence.xml配置:

    <persistence-unit name="org.persistence.jpa" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/BitronixJTADataSource</jta-data-source>
        <class>org.drools.persistence.info.SessionInfo</class>
        <class>org.drools.persistence.info.WorkItemInfo</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.jndi.class" value="bitronix.tm.jndi.BitronixInitialContextFactory"/> 
            <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.BTMTransactionManagerLookup" />
        </properties>
    </persistence-unit>
    

    jndi.properties: java.naming.factory.initial的= bitronix.tm.jndi.BitronixInitialContextFactory

    问题是:我得到这个堆栈:

    javax.persistence.PersistenceException: [PersistenceUnit: org.drools.persistence.jpa] Unable to build EntityManagerFactory
    [...]
    Caused by: org.hibernate.service.jndi.JndiException: Error parsing JNDI name [jdbc/BitronixJTADataSource]
    [...]
    Caused by: javax.naming.OperationNotSupportedException
    [...]
    

    所以肯定有些不对劲。你能救我吗?

2 个答案:

答案 0 :(得分:1)

我可以通过使用Hibernate 3.x而不是4.x来解决这个问题 - &gt;事情必定已经改变,但我没有进一步调查。

答案 1 :(得分:-1)

“我可以通过使用Hibernate 3.x而不是4.x来解决这个问题 - &gt;某些事情必须改变,但我没有进一步调查。”

通过“可能”你的意思是“我认为”或者你的意思是你让它与Hibernate 3.x一起使用?