层层解析Hibernate中的事务的滥用

开发 后端
本文从四个方面来讲述Hibernate事务的滥用,结合具体实例,告诉您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,也就是说这样写:
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中的事务的滥用

【编辑推荐】

  1. Hibernate框架如何实现ORM
  2. Hibernate/JPA成功使用的十点心得
  3. 优化Hibernate性能的几点建议
责任编辑:张攀 来源: javaeye论坛
相关推荐

2009-06-03 10:20:11

Hibernate事务管理配置

2009-06-30 16:41:12

Hibernate的事

2009-09-24 13:25:58

Hibernate m

2009-09-22 10:50:04

Hibernate c

2009-09-23 17:41:05

Hibernate事务

2009-09-25 12:59:53

Hibernate事务

2009-06-03 10:02:53

Hibernate批量删除

2009-09-25 11:27:19

Hibernate J

2009-06-17 16:08:58

Hibernate C

2009-06-26 16:15:04

Criteria的用法Hibernate

2009-06-18 10:07:03

CriteriaHibernate

2017-01-19 15:32:36

Java全局事务本地事务

2009-09-24 14:12:22

Hibernate数据

2009-09-22 16:49:42

Hibernate分页

2009-06-16 16:27:22

Hibernate优化

2009-09-23 15:12:41

Hibernate视图

2009-06-26 10:19:00

Clob字段Hibernate

2009-06-17 14:17:40

Criteria条件查Hibernate

2009-09-22 15:26:30

Hibernate多表

2009-06-29 08:59:05

hbm的generat
点赞
收藏

51CTO技术栈公众号