小編給大家分享一下Hibernate中的事務(wù)怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比噶爾網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式噶爾網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋噶爾地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。
Hibernate事務(wù)的使用:
一、要使用事務(wù)?
事務(wù)是單個(gè)的工作單元。如果某一事務(wù)成功,則在該事務(wù)中進(jìn)行的所有數(shù)據(jù)修改均會(huì)提交,成為數(shù)據(jù)庫中的***組成部分。如果事務(wù)遇到錯(cuò)誤且必須取消或回滾,則所有數(shù)據(jù)修改均被清除。
(查詢中一般可以不用事務(wù)!除非查詢和修改是合成一個(gè)事務(wù)的操作)
二、不用事務(wù)?(今天重點(diǎn)說這里)
Hibernate每次都更新數(shù)據(jù)庫,并且使用了。
1、Hibernate事務(wù)會(huì)自動(dòng)更新緩存。其意思是指,緩存中查出來的所有對(duì)象(一個(gè)事務(wù)內(nèi)),都判斷對(duì)象是否被更新。如果更新了,Hibernate會(huì)自動(dòng)更新數(shù)據(jù)庫。
事務(wù)開始.
假設(shè)我現(xiàn)在查出所有用戶,并對(duì)某些用戶進(jìn)行了操作,但這些操作我不想更新數(shù)據(jù)庫,也就是我只是操作緩存,來計(jì)算他們的獎(jiǎng)金。
計(jì)算完成獎(jiǎng)金后,該方法結(jié)束。
事務(wù)結(jié)束.
以上操作的結(jié)果是,數(shù)據(jù)庫記錄被更新了。因?yàn)樵谝粋€(gè)事務(wù)里,但其實(shí)我只是想試操作用戶的數(shù)據(jù)而不想更新。但為什么更新了,這就要問Hibernate了,對(duì)是這家伙干的好事,Hibernate發(fā)現(xiàn)查出了所有用戶的List,并把所有用戶都放在Hibernate緩存中了。如果在一個(gè)事務(wù)中,你對(duì)該List里的用戶操作了,并且他發(fā)現(xiàn)了你的操作,他會(huì)自動(dòng)更新數(shù)據(jù)庫。
但我現(xiàn)在不想讓他更新數(shù)據(jù)庫,并且我的計(jì)算獎(jiǎng)金算法看上去沒有必要用事務(wù)。是的他根本沒有必要用試務(wù),因?yàn)樗桓?,不保存,他只是算?jiǎng)金。
所以解決這個(gè)問題的方法就是不要用管理器中對(duì)該Service進(jìn)行管理.
現(xiàn)在想想為什么我們不用事務(wù),Hibernate就不自動(dòng)更新數(shù)據(jù)庫了?原因很簡單,因?yàn)楫?dāng)我們查出所有用戶之后Hibernate還做了件事,Session.close();
當(dāng)他調(diào)用了Session.close()方法時(shí),我們查詢的List就變成了游離對(duì)象了,這時(shí)該List已不在Hibernate緩存中,這時(shí)你可以方便的設(shè)置和更新了,只不手動(dòng)設(shè)用Update
Hibernate是不會(huì)幫你更新的^_^。當(dāng)然也可以使用evict方法把對(duì)象變?yōu)橛坞x。
呵呵,可是在一個(gè)事務(wù)中,就算你把他變成游離,Hibernate還是會(huì)自動(dòng)更新的。(我試過)不過估計(jì)Hibernate應(yīng)該有設(shè)置在一個(gè)事務(wù)內(nèi)不自動(dòng)更新的參數(shù)吧?
三、Hibernate事Spring事務(wù)配置說明:
我下面用的是[指定對(duì)滿足哪些beanname的bean自動(dòng)生成事務(wù)代理],這里看清楚點(diǎn)是name,也就是說這樣寫:
表示在Spring上下文中有userInfoService實(shí)例,并且這個(gè)實(shí)例必需對(duì)應(yīng)這樣一個(gè)接口UserInfoService,這時(shí)Spring才會(huì)自動(dòng)為其配置Hibernate事務(wù),否則會(huì)報(bào)錯(cuò)
[org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype],錯(cuò)誤的意思是找不到對(duì)應(yīng)的接口。
四、下面是我的Spring配置文件
五、下面是詳細(xì)的錯(cuò)誤信息 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) 調(diào)用父類構(gòu)造方法...! 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中的事務(wù)怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!