有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

HSQLDB中的java事务忽略自动提交

在应用程序的单元测试中,我想使用HSQLDB测试持久层。然而,这要求我在事务对象上显式提交。在其中一个测试中,这不会使foo实例持久化:

final Foo foo = new Foo("someValue");

final EntityManagerFactory factory = Persistence.createEntityManagerFactory("TestDataModel");
final EntityManager em = factory.createEntityManager();

em.persist(foo);

相反,每次我想要持久化和实体时,我都必须启动并提交一个事务:

final Foo foo = new Foo("someValue");

final EntityManagerFactory factory = Persistence.createEntityManagerFactory("TestDataModel");
final EntityManager em = factory.createEntityManager();
final EntityTransaction et = em.getTransaction();

et.begin();
em.persist(foo);
et.commit();

我没有在我的应用程序中使用任何@Transactional注释。我最终想要测试的类是jax-rs应用程序中的无状态服务(EntityManager是使用Needle注入的),它不需要我在存储实体时开始和提交事务。网络上的一些人说,这个问题与hsqldb中的延迟写入有关,但我已将其添加到连接url中,但没有产生任何差异。我还在Hibernate配置文件中启用了autocommit,并将刷新模式设置为always

坚持。xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="TestDataModel" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>Foo</class>

        <properties>
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file:db/db;shutdown=true;hsqldb.write_delay_millis=0;hsqldb.write_delay=false;" />
            <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.flushMode" value="ALWAYS" />
            <property name="hibernate.connection.autocommit" value="true" />

        </properties>
    </persistence-unit>
</persistence>

共 (0) 个答案