houfeng_dao 2008-12-17 11:03
浏览 218
已采纳

spring集成hibernate,事务处理错误问题

小弟初学hibernate,用spring集成hibernate时候,主动加载事务处理 ,
出现错误,向大家请教。

[color=red]Manager类代码:[/color]

[color=red]OrgManagerImpl 继承 HibernateDaoSupport
其中findOrg方法代码为[/color]
public Organization findOrg(int orgId) {
// TODO Auto-generated method stub
return (Organization) getHibernateTemplate().[color=red]get/color;
}

[color=red]此处get,load效果一样,都报错。[/color]

[color=red]OrgManagerImpl Spring bean 配置文件 applicationContext-beans.xml[/color]

<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="OrgManager" class="com.ddy.oa.manager.impl.OrgManagerImpl" >
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

[color=red]spring 事务处理 xml文件 : applicationContext-common.xml[/color]

<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置SessionFactory -->


classpath:hibernate.cfg.xml

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property> 
</bean>    

<!-- 事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="del*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="find*" propagation="REQUIRED"/>
        <tx:method name="*" read-only="true"/>
    </tx:attributes>
</tx:advice>

<!-- 那些类那些方法使用事务 -->
<aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution(* com.ddy.oa.manager.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>

[color=red]采用 JUnit测试类 代码[/color]

public void testFindOrg() {
OrgManager orgImpl = (OrgManager)factory.getBean("OrgManager");
Organization org = (Organization)orgImpl.findOrg(2);
System.out.println(org.getSn() + "--> parent " + org.getParent().getDescription());
}

[color=red]报错信息:[/color]

10:48:06,531 WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/F:/Eclipse3.3/OA/myOa/WebRoot/WEB-INF/lib/ehcache-1.2.jar!/ehcache-failsafe.xml
10:48:08,421 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:160)
at com.ddy.oa.model.Organization$$EnhancerByCGLIB$$16ae7ba.getSn()
at com.ddy.oa.manager.impl.OrgManagerImplTest.testFindOrg(OrgManagerImplTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

已经想了好几天,还是不能够解决问题,向大家请教。先谢啦。

[b]问题补充:[/b]
先感谢 dmewy 的回答。

OrgManagerImpl 是一个Manager类,我用它的findOrg方法来为业务逻辑来提供

Organization 的查找。不可能把业务逻辑也写倒方法里的。

就是根据id 来查找 倒具体的Organization 类返回啊。

[color=red]或者可以考虑open session in view 模式也可以解决。 [/color]

可以请教一下 open session in view 模式 具体怎么设置嘛?

  • 写回答

5条回答 默认 最新

  • iteye_5200 2008-12-17 15:08
    关注

    [code="java"]public void testFindOrg() {
    OrgManager orgImpl = (OrgManager)factory.getBean("OrgManager");
    Organization org = (Organization)orgImpl.findOrg(2);
    System.out.println(org.getSn() + "--> parent " + org.getParent().getDescription());
    }[/code]
    System.out.println(org.getSn() + "--> parent " + org.getParent().getDescription());
    这段代码应该就是抛LazyInitializationException:19 - could not initialize proxy - the owning Session was closed 的地方把,出这个异常的原因就在于Organization org = (Organization)orgImpl.findOrg(2); 这个调用结束后数据库事务结束,hibernate session关闭,然后你调用org.getParent().getDescription()); 肯定就异常了啊,你把这段话放在
    [code="java"]public Organization findOrg(int orgId) {
    // TODO Auto-generated method stub
    return (Organization) getHibernateTemplate().get(Organization.class,
    orgId);
    } [/code]里面调用就没有问题了。或者可以考虑open session in view 模式也可以解决。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误