您好,登錄后才能下訂單哦!
小編給大家分享一下Hibernate中的事務怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
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事務配置說明:
我下面用的是[指定對滿足哪些beanname的bean自動生成事務代理],這里看清楚點是name,也就是說這樣寫:
表示在Spring上下文中有userInfoService實例,并且這個實例必需對應這樣一個接口UserInfoService,這時Spring才會自動為其配置Hibernate事務,否則會報錯
[org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy14]torequiredtype],錯誤的意思是找不到對應的接口。
四、下面是我的Spring配置文件
五、下面是詳細的錯誤信息 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中的事務怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。