Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96862
  • 博文数量: 73
  • 博客积分: 3971
  • 博客等级: 中校
  • 技术积分: 875
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-09 20:57
文章分类

全部博文(73)

文章存档

2008年(73)

我的朋友
最近访客

分类:

2008-06-27 09:45:49

Hibernate事务的使用:

一、要使用事务?
    事务是单个的工作单元。如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的永久组成部分。如果事务遇到错误且必须取消或回滚,则所有数据

修改均被清除。
    (查询中一般可以不用事务!除非查询和修改是合成一个事务的操作)


二、不用事务?(今天重点说这里)
    Hibernate每次都更新数据库,并且使用了。
    1、Hibernate事务会自动更新缓存。其意思是指,缓存中查出来的所有对象(一个事务内),都判断对象是否被更新。
如果更新了,Hibernate会自动更新数据库。
  
事务开始.

假设我现在查出所有用户,并对某些用户进行了操作,但这些操作我不想更新数据库,也就是我只是操作缓存,来计算他们的奖金。

计算完成奖金后,该方法结束。

事务结束.

以上操作的结果是,数据库记录被更新了。因为在一个事务里,但其实我只是想试操作用户的数据而不想更新。但为什么更新了,这就要问Hibernate了,对是这家伙干的

好事,Hibernate发现查出了所有用户的List,并把所有用户都放在Hibernate缓存中了。如果在一个事务中,你对该List里的用户操作了,并且他发现了你的操作,他会自动更新数

据库。
但我现在不想让他更新数据库,并且我的计算奖金算法看上去没有必要用事务。是的他根本没有必要用试务,因为他不更新,不保存,他只是算奖金。

所以解决这个问题的方法就是不要用管理器中对该Service进行管理.

现在想想为什么我们不用事务,Hibernate就不自动更新数据库了? 原因很简单,因为当我们查出所有用户之后Hibernate还做了件事,Session.close();
当他调用了Session.close()方法时,我们查询的List就变成了游离对象了,这时该List已不在Hibernate缓存中,这时你可以方便的设置和更新了,只不手动设用Update

Hibernate是不会帮你更新的^_^。当然也可以使用evict方法把对象变为游离。


呵呵,可是在一个事务中,就算你把他变成游离,Hibernate还是会自动更新的。(我试过)不过估计Hibernate应该有设置在一个事务内不自动更新的参数吧?

三、Hibernate事Spring事务配置说明:
我下面用的是 [指定对满足哪些bean name的bean自动生成事务代理],这里看清楚点是name,也就是说这样写:
userInfoService
表示在Spring上下文中有userInfoService实例,并且这个实例必需对应这样一个接口UserInfoService,这时Spring才会自动为其配置Hibernate事务,否则会报错

[org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy14] to required type ],
错误的意思是找不到对应的接口。

四、下面是我的Spring配置文件
   
       
           
       

       
           
     com/insurance/model/base/UserInfo.hbm.xml
     com/insurance/model/base/Base.hbm.xml
     com/insurance/model/base/AdminInfo.hbm.xml
     com/insurance/model/base/CompanyInfo.hbm.xml  
     com/insurance/model/base/BxCom.hbm.xml
     com/insurance/model/base/XzInfo.hbm.xml
     com/insurance/model/base/ClientInfo.hbm.xml
     com/insurance/model/base/UserGrade.hbm.xml
     com/insurance/model/base/UserMoneyLog.hbm.xml
     com/insurance/model/base/SxBdInfo.hbm.xml
     com/insurance/model/base/CxBdInfo.hbm.xml
     com/insurance/model/base/CarBdInfo.hbm.xml
     com/insurance/model/base/SelectBase.hbm.xml
     com/insurance/model/base/PayInfo.hbm.xml
     com/insurance/model/base/AwardInfo.hbm.xml   

           

       

       
           
                    ${hibernate.dialect}
   true  
       ${hibernate.show_sql}
   org.hibernate.hql.classic.ClassicQueryTranslatorFactory
                   ${hibernate.hbm2ddl.auto}
                ${hibernate.jdbc.batch_size}
                ${hibernate.jdbc.fetch_size}
           

       

   

   
   
       
   

   
   
    
       
   
     
     
       PROPAGATION_REQUIRED,readOnly
      PROPAGATION_REQUIRED
     

    

   
   
    
    
           
           
           
            userInfoService
            adminInfoService
            pageService
            companyInfoService
            xzInfoService
            baseService  
            clientInfoService
            userGradeService
            userMoneyLogService
            sxBdInfoService
            cxBdInfoService
            carBdInfoService
    
        
                   
           

           
    

       
       
           
               
                transactionInterceptor
           

       

   


五、下面是详细的错误信息
2008-05-23 09:07:22,046 [org.springframework.web.context.ContextLoader]-[ERROR] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountAddPay' defined in ServletContext resource [/WEB-

INF/classes/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException:

PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert

property value of type [$Proxy14] to required type [com.insurance.service.pay.AccountSellPay] for property 'accountSellPay']
PropertyAccessExceptionsException (1 errors)
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy14] to required type

[com.insurance.service.pay.AccountSellPay] for property 'accountSellPay'
        at org.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary(BeanWrapperImpl.java:951)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:692)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1057)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
        at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
        at org.springframework.web.context.ContextLoaderServlet.init(ContextLoaderServlet.java:83)
        at javax.servlet.GenericServlet.init(GenericServlet.java:211)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4335)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)
        at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277)
        at org.apache.catalina.core.StandardHost.install(StandardHost.java:832)
        at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:964)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
        at org.apache.catalina.core.StandardService.start(StandardService.java:476)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:2298)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
        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:585)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422)
调用父类构造方法...!
2008-05-23 09:07:22,656 [org.apache.struts.validator.ValidatorPlugIn]-[INFO] Loading validation rules file from '/WEB-INF/validator-rules.xml'
2008-05-23 09:07:22,656 [org.apache.struts.validator.ValidatorPlugIn]-[INFO] Loading validation rules file from '/WEB-INF/validation.xml'
2008-05-23 09:07:22,906 [org.springframework.web.struts.ContextLoaderPlugIn]-[INFO] ContextLoaderPlugIn for Struts ActionServlet 'action, module '':

initialization started
2008-05-23 09:07:22,906 [org.springframework.web.struts.ContextLoaderPlugIn]-[ERROR] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountAddPay' defined in ServletContext resource [/WEB-

INF/classes/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException:

PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.TypeMismatchException: Failed to convert

property value of type [$Proxy14] to required type [com.insurance.service.pay.AccountSellPay] for property 'accountSellPay']
PropertyAccessExceptionsException (1 errors)
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy14] to required type

[com.insurance.service.pay.AccountSellPay] for property 'accountSellPay'
        at org.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary(BeanWrapperImpl.java:951)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:692)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764)
        at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1057)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313)
        at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184)
        at org.springframework.web.context.ContextLoaderServlet.init(ContextLoaderServlet.java:83)
        at javax.servlet.GenericServlet.init(GenericServlet.java:211)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4335)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)
        at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277)
        at org.apache.catalina.core.StandardHost.install(StandardHost.java:832)
        at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:964)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
        at org.apache.catalina.core.StandardService.start(StandardService.java:476)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:2298)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
        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:585)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422)
javax.servlet.UnavailableException
        at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:880)
        at org.apache.struts.action.ActionServlet.init(ActionServlet.java:336)
        at com.insurance.struts.action.ActionInit.init(ActionInit.java:34)
        at javax.servlet.GenericServlet.init(GenericServlet.java:211)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4335)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)
        at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277)
        at org.apache.catalina.core.StandardHost.install(StandardHost.java:832)
        at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:964)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
        at org.apache.catalina.core.StandardService.start(StandardService.java:476)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:2298)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
        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:585)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422)
2008-5-23 9:07:23 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2008-5-23 9:07:23 org.apache.jk.common.ChannelSocket init
信息: JK2: ajp13 listening on /0.0.0.0:8009
2008-5-23 9:07:23 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/32 config=D:\Tomcat5.0\conf\jk2.properties
2008-5-23 9:07:23 org.apache.catalina.startup.Catalina start
信息: Server startup in 6953 ms

阅读(887) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~