houfeng_dao
2008-12-17 11:03 阅读 200
已采纳

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 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 模式也可以解决。

    点赞 评论 复制链接分享
  • dmewy dmewy 2008-12-17 11:45

    应该是说你某处的connection对象release掉了,但是后面还有东西用它..

    点赞 评论 复制链接分享
  • iteye_5200 iteye_5200 2008-12-18 08:46

    [code="java"]

    OpenSessionInViewFilter

    org.springframework.orm.hibernate.support.OpenSessionInViewFilter



    singleSession

    false



    <filter-mapping>   
        <filter-name>OpenSessionInViewFilter</filter-name>   
        <url-pattern>/*</url-pattern>   
    </filter-mapping>[/code]
    

    在web.xml里面加个filter就可以了,更加详细的结束可以在论坛里面找找。

    点赞 评论 复制链接分享
  • dch1287 dch1287 2008-12-21 23:04

    楼上回答很详细了 楼主的问题是hibernate的 lazy load 造成的
    如果不嫌麻烦 也可以
    [code="java"]
    public void testFindOrg() {
    OrgManager orgImpl = (OrgManager)factory.getBean("OrgManager");
    Organization org = (Organization)orgImpl.findOrg(2);
    [color=red]Hibernate.Initialize(org);[/color]
    System.out.println(org.getSn() + "--> parent " +
    org.getParent().getDescription());
    }
    [/code]

    点赞 评论 复制链接分享
  • dch1287 dch1287 2008-12-21 23:07

    JavaEye 的 [code] 里面没法用颜色啊

    这个问题什么时候能解决啊 用了

     
    了吧 呵呵

    Robbin 等大大们 找个解决方案嘛

    楼上的 代码中 第四行中的 和 可以无视
    只要注意
    [color=red][b]Hibernate.Initialize(org);[/b][/color]
    就可以了

    点赞 评论 复制链接分享

相关推荐