Hibernate事务的使用:
坚守“ 做人真诚 · 做事靠谱 · 口碑至上 · 高效敬业 ”的价值观,专业网站建设服务10余年为成都成都纯水机小微创业公司专业提供企业网站制作营销网站建设商城网站建设手机网站建设小程序网站建设网站改版,从内容策划、视觉设计、底层架构、网页布局、功能开发迭代于一体的高端网站建设服务。
一、要使用事务?
事务是单个的工作单元。如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的***组成部分。如果事务遇到错误且必须取消或回滚,则所有数据修改均被清除。
(查询中一般可以不用事务!除非查询和修改是合成一个事务的操作)
二、不用事务?(今天重点说这里)
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事务配置说明:
我下面用的是[指定对满足哪些beanname的bean自动生成事务代理],这里看清楚点是name,也就是说这样写:
userInfoService
表示在Spring上下文中有userInfoService实例,并且这个实例必需对应这样一个接口UserInfoService,这时Spring才会自动为其配置Hibernate事务,否则会报错
[org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype],错误的意思是找不到对应的接口。
四、下面是我的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-2309:07:22,046[org.springframework.web.context.ContextLoader]-[ERROR]Contextinitializationfailed org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'accountAddPay'definedinServletContextresource[/WEB- INF/classes/applicationContext.xml]:Errorsettingpropertyvalues;nestedexceptionisorg.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException(1errors);nestedpropertyAccessExceptionsare:[org.springframework.beans.TypeMismatchException:Failedtoconvert propertyvalueoftype[$Proxy14]torequiredtype[com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay'] PropertyAccessExceptionsException(1errors) org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype [com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay' atorg.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary(BeanWrapperImpl.java:951) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:692) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1057) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283) atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313) atorg.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139) atorg.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246) atorg.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184) atorg.springframework.web.context.ContextLoaderServlet.init(ContextLoaderServlet.java:83) atjavax.servlet.GenericServlet.init(GenericServlet.java:211) atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019) atorg.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862) atorg.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991) atorg.apache.catalina.core.StandardContext.start(StandardContext.java:4335) atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823) atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807) atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:595) atorg.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277) atorg.apache.catalina.core.StandardHost.install(StandardHost.java:832) atorg.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683) atorg.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432) atorg.apache.catalina.startup.HostConfig.start(HostConfig.java:964) atorg.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349) atorg.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091) atorg.apache.catalina.core.StandardHost.start(StandardHost.java:789) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083) atorg.apache.catalina.core.StandardEngine.start(StandardEngine.java:478) atorg.apache.catalina.core.StandardService.start(StandardService.java:476) atorg.apache.catalina.core.StandardServer.start(StandardServer.java:2298) atorg.apache.catalina.startup.Catalina.start(Catalina.java:556) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) atjava.lang.reflect.Method.invoke(Method.java:585) atorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284) atorg.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422) 调用父类构造方法...! 2008-05-2309:07:22,656[org.apache.struts.validator.ValidatorPlugIn]-[INFO]Loadingvalidationrulesfilefrom'/WEB-INF/validator-rules.xml' 2008-05-2309:07:22,656[org.apache.struts.validator.ValidatorPlugIn]-[INFO]Loadingvalidationrulesfilefrom'/WEB-INF/validation.xml' 2008-05-2309:07:22,906[org.springframework.web.struts.ContextLoaderPlugIn]-[INFO]ContextLoaderPlugInforStrutsActionServlet'action,module'': initializationstarted 2008-05-2309:07:22,906[org.springframework.web.struts.ContextLoaderPlugIn]-[ERROR]Contextinitializationfailed org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'accountAddPay'definedinServletContextresource[/WEB- INF/classes/applicationContext.xml]:Errorsettingpropertyvalues;nestedexceptionisorg.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException(1errors);nestedpropertyAccessExceptionsare:[org.springframework.beans.TypeMismatchException:Failedtoconvert propertyvalueoftype[$Proxy14]torequiredtype[com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay'] PropertyAccessExceptionsException(1errors) org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype [com.insurance.service.pay.AccountSellPay]forproperty'accountSellPay' atorg.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary(BeanWrapperImpl.java:951) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:692) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572) atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764) atorg.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1057) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233) atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145) atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:283) atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:313) atorg.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:139) atorg.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:246) atorg.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:184) atorg.springframework.web.context.ContextLoaderServlet.init(ContextLoaderServlet.java:83) atjavax.servlet.GenericServlet.init(GenericServlet.java:211) atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019) atorg.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862) atorg.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991) atorg.apache.catalina.core.StandardContext.start(StandardContext.java:4335) atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823) atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807) atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:595) atorg.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277) atorg.apache.catalina.core.StandardHost.install(StandardHost.java:832) atorg.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683) atorg.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432) atorg.apache.catalina.startup.HostConfig.start(HostConfig.java:964) atorg.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349) atorg.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091) atorg.apache.catalina.core.StandardHost.start(StandardHost.java:789) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083) atorg.apache.catalina.core.StandardEngine.start(StandardEngine.java:478) atorg.apache.catalina.core.StandardService.start(StandardService.java:476) atorg.apache.catalina.core.StandardServer.start(StandardServer.java:2298) atorg.apache.catalina.startup.Catalina.start(Catalina.java:556) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) atjava.lang.reflect.Method.invoke(Method.java:585) atorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284) atorg.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422) javax.servlet.UnavailableException atorg.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:880) atorg.apache.struts.action.ActionServlet.init(ActionServlet.java:336) atcom.insurance.struts.action.ActionInit.init(ActionInit.java:34) atjavax.servlet.GenericServlet.init(GenericServlet.java:211) atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019) atorg.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862) atorg.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991) atorg.apache.catalina.core.StandardContext.start(StandardContext.java:4335) atorg.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823) atorg.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807) atorg.apache.catalina.core.StandardHost.addChild(StandardHost.java:595) atorg.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277) atorg.apache.catalina.core.StandardHost.install(StandardHost.java:832) atorg.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:683) atorg.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432) atorg.apache.catalina.startup.HostConfig.start(HostConfig.java:964) atorg.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349) atorg.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091) atorg.apache.catalina.core.StandardHost.start(StandardHost.java:789) atorg.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083) atorg.apache.catalina.core.StandardEngine.start(StandardEngine.java:478) atorg.apache.catalina.core.StandardService.start(StandardService.java:476) atorg.apache.catalina.core.StandardServer.start(StandardServer.java:2298) atorg.apache.catalina.startup.Catalina.start(Catalina.java:556) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) atjava.lang.reflect.Method.invoke(Method.java:585) atorg.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284) atorg.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422) 2008-5-239:07:23org.apache.coyote.http11.Http11Protocolstart 信息:StartingCoyoteHTTP/1.1onhttp-8080 2008-5-239:07:23org.apache.jk.common.ChannelSocketinit 信息:JK2:ajp13listeningon/0.0.0.0:8009 2008-5-239:07:23org.apache.jk.server.JkMainstart 信息:JkrunningID=0time=0/32config=D:\Tomcat5.0\conf\jk2.properties 2008-5-239:07:23org.apache.catalina.startup.Catalinastart 信息:Serverstartupin6953ms
您正在阅读:层层解析Hibernate中的事务的滥用
文章名称:层层解析Hibernate中的事务的滥用
地址分享:http://www.shufengxianlan.com/qtweb/news20/185620.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联