从HibernateTransactionManager迁移到jpattransactionmanager



由于各种原因从

更改了现有应用程序中的sessionFactory bean创建:
<bean id="sessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
       <property name="mappingLocations" ref="hibernateMappingFiles" />
       <property name="hibernateProperties" ref="hibernateProperties" />
   </bean>
   <tx:annotation-driven transaction-manager="transactionManager" />
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

:

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="spring-jpa" />
    </bean>
    <bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory" />
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

这是我新添加的持久性单元:

<persistence-unit name="spring-jpa">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="none" />
            <property name="javax.persistence.validation.mode" value="none" />
            <property name="hibernate.jdbc.use_streams_for_binary"
                value="true" />
            <property name="hibernate.jdbc.fetch_size" value="100" />
            <property name="hibernate.current_session_context_class" value="org.springframework.orm.hibernate3.SpringSessionContext" />
            <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" />
        </properties>
    </persistence-unit>

之后,我在许多Junit集成测试的事务块中收到一个InvalidComponentException。例如:

createFolder ( "/examples/inheritance" );
transactionTemplate.execute ( new TransactionCallbackWithoutResult () {
            @Override
            protected void doInTransactionWithoutResult ( TransactionStatus status ) {
                for ( Resource r : Arrays.asList ( abstractComponent, concreteAComponent, concreteBComponent ) ) {
                    ComponentVersion c;
                    try {
                        c = componentService.validateAssignValues ( new ModelSource ( loadResource ( r ) ), null, TechnicalVersion.INITIAL_VERSION,
                                ModelVersion.INITIAL_VERSION, ModelVersionIncrement.MAJOR );
                    } catch ( InvalidComponentException e ) {
                        fail ( "Invalid Component: " + e.getMessage () );
                        return;
                    } catch ( IOException e ) {
                        fail ( "IOException: " + e.getMessage () );
                        return;
                    }
                    sessionFactory.getCurrentSession ().save ( c.getModelElement () );
                    sessionFactory.getCurrentSession ().save ( c );
                }
            }
        } );

在:

抛出异常

catch (InvalidComponentException e) {

java.lang.AssertionError: Invalid Component: The inherited component does not exist: name=/examples/inheritance/AbstractComponent
    at org.junit.Assert.fail(Assert.java:93)
    at info.server.component.FindComponentHierarchyTest$1.doInTransactionWithoutResult(FindComponentHierarchyTest.java:83)
    at org.springframework.transaction.support.TransactionCallbackWithoutResult.doInTransaction(TransactionCallbackWithoutResult.java:33)
    ...

问题在sessionFactory。getCurrentSession()。保存(c);我只能调用:

Session session = sessionFactory.getCurrentSession ();
        session.save ( c );
        session.flush ();

现在我知道HibernateTransactionManager的工作方式与jpattransactionmanager不同。但是,如果我必须每次都手动调用session.flush(),那将是一个痛苦和性能杀手。值得注意的是,当我试图删除一个对象时,我也遇到了同样的问题。我必须合并,然后删除或拆分为2个事务。

我如何配置JpaTransactionManager来解决这个问题,而不添加session.flush()?我在新添加的persistence.xml中尝试了一下,但是没有帮助。如果有任何帮助,我将不胜感激。


我刚刚检查了我的实际冲洗模式是什么,有两个变体,它是AUTO。我试着调用em.setFlushMode(FlushModeType.COMMIT),但冲洗模式根本没有改变,它总是AUTO和((Session)em.getDelegate()).setFlushMode(FlushMode.MANUAL)我得到这个异常:

org.hibernate.SessionException: Session is closed!
    at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:72)
    at org.hibernate.impl.SessionImpl.setFlushMode(SessionImpl.java:1423)
    at info.novatec.np.server.component.FindComponentHierarchyTest.importComponents(FindComponentHierarchyTest.java:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

请注意,您给了我们一个不完整的堆栈跟踪,因为您只打印异常消息和断言错误,但没有向我们显示真正的异常堆栈(执行e.p intstacktrace()或catch中的其他操作)。无论如何,这看起来像是一个商业,非技术/hibernate/jpa异常,所以我不认为它会带来任何解决方案。

如果它与flush()一起工作,这可能意味着你现在在flushmode=MANUAL,当你可能在flushmode=AUTO.

Hibernate的可用选项如下所示:org.hibernate.FlushMode顺便说一句,似乎手动刷新模式不是JPA规范的一部分:javax.persistence.FlushModeType

所以你可以试着检查你实际的冲洗模式是什么。例如:

em.getFlushMode()

你还应该检查结果:

((Session)em.getDelegage()).getFlushMode()

因为您正在导入Hibernate配置文件Hibernate .cfg.xml。也许在hibernate中有一些错误,没有设置适当的flushmode或在某些特定情况下,如您的。

检查是否你的flushmode是一致的所有应用程序(当你不显式修改它…),因为如果你使用Spring @Transactionnal属性"readonly=true"或一些类似的东西,hibernate flushmode是自动设置为手动。如果你不使用这些注释,也许在2003年的事务回调系统中处理事务是一个好主意。


实际上好像有一个JpaTemplate

请看下面的代码:http://massapi.com/source/apache-camel-2.5.0/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaTemplateTransactionStrategy.java.html

根据Javadoc, TransactionTemplate用于处理低级资源,如JDBC。你应该使用适合你需要的模板,否则你可能会有奇怪的副作用。

相关内容

  • 没有找到相关文章

最新更新