Hibernate事务

问题描述

如果我没用spring该怎么管理Hibernate的事务..望高手指点...小弟雪地等待最好有个例子

解决方案

本帖最后由 a_qazwsx 于 2009-10-31 21:27:09 编辑
解决方案二:
自己写个Hibernate Factory类吧,importorg.hibernate.HibernateException;importorg.hibernate.Session;importorg.hibernate.cfg.Configuration;/***ConfiguresandprovidesaccesstoHibernatesessions,tiedtothe*currentthreadofexecution.FollowstheThreadLocalSession*pattern,see{@linkhttp://hibernate.org/42.html}.*/publicclassHibernateSessionFactory{/***Locationofhibernate.cfg.xmlfile.*LocationshouldbeontheclasspathasHibernateuses*#resourceAsStreamstylelookupforitsconfigurationfile.*Thedefaultclasspathlocationofthehibernateconfigfileis*inthedefaultpackage.Use#setConfigFile()toupdate*thelocationoftheconfigurationfileforthecurrentsession.*/privatestaticStringCONFIG_FILE_LOCATION="/hibernate.cfg.xml";privatestaticfinalThreadLocal<Session>threadLocal=newThreadLocal<Session>();privatestaticConfigurationconfiguration=newConfiguration();privatestaticorg.hibernate.SessionFactorysessionFactory;privatestaticStringconfigFile=CONFIG_FILE_LOCATION;static{try{configuration.configure(configFile);sessionFactory=configuration.buildSessionFactory();}catch(Exceptione){System.err.println("%%%%ErrorCreatingSessionFactory%%%%");e.printStackTrace();}}privateHibernateSessionFactory(){}/***ReturnstheThreadLocalSessioninstance.Lazyinitialize*the<code>SessionFactory</code>ifneeded.**@returnSession*@throwsHibernateException*/publicstaticSessiongetSession()throwsHibernateException{Sessionsession=(Session)threadLocal.get();if(session==null||!session.isOpen()){if(sessionFactory==null){rebuildSessionFactory();}session=(sessionFactory!=null)?sessionFactory.openSession():null;threadLocal.set(session);}returnsession;}/***Rebuildhibernatesessionfactory**/publicstaticvoidrebuildSessionFactory(){try{configuration.configure(configFile);sessionFactory=configuration.buildSessionFactory();}catch(Exceptione){System.err.println("%%%%ErrorCreatingSessionFactory%%%%");e.printStackTrace();}}/***Closethesinglehibernatesessioninstance.**@throwsHibernateException*/publicstaticvoidcloseSession()throwsHibernateException{Sessionsession=(Session)threadLocal.get();threadLocal.set(null);if(session!=null){session.close();}}/***returnsessionfactory**/publicstaticorg.hibernate.SessionFactorygetSessionFactory(){returnsessionFactory;}/***returnsessionfactory**sessionfactorywillberebuildedinthenextcall*/publicstaticvoidsetConfigFile(StringconfigFile){HibernateSessionFactory.configFile=configFile;sessionFactory=null;}/***returnhibernateconfiguration**/publicstaticConfigurationgetConfiguration(){returnconfiguration;}

调用的时候Sessionsession=HibernateSessionFactory.getSession();Transactiontx=session.beginTransaction();session.save(user);tx.commit();HibernateSessionFactory.closeSession();

解决方案三:
good
解决方案四:
加入hibernate环境的会创建一个类里面有得到连接还有关闭的连接事务帮你弄好了直接调用
解决方案五:
学习
解决方案六:
1楼理解错题意了吧楼主说的是spring管理hibernate的事务
解决方案七:
定义一个filter,在filter内创建session开启Transaction,分发给相应线程的请求,如果在处理的过程中游异常发生,就在filter中截获异常,并将Transaction回滚,之后将异常记录到日志中!
解决方案八:
MyEclipse的hibernate插件会自动给你生成一个hibernatesessionfactory类

时间: 2024-09-19 08:56:59

Hibernate事务的相关文章

struts2-spring配置方式管理hibernate事务的情况下,每过一段时间产生的错误

问题描述 spring配置方式管理hibernate事务的情况下,每过一段时间产生的错误 项目的背景是struts2+spring+hibernate3 weblogic服务器集群环境 数据库oracle spring管理hibernate 采用配置的方式,配置在了service层. 数据库连接方面使用了hibernate自带的连接池(用其他第三方连接池也试了,问题依旧) 现象:每过一段时间便会报错:(部分) jdbc rollback failed ... TransactionExcepti

hibernate事务开启不成功的错误

问题描述 hibernate事务开启不成功的错误 package cn.jbit.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.

CGLIB实现的一个hibernate事务管理代理类出现的问题,以及一些疑问

问题描述 先声明下,本人系菜鸟一个,问的问题如果类似于1+1为什么等于2,请少拍板这2天在学习CGLIB,一时兴起用CGLIB实现了一个管理hibernate事务的类.代码如下:package com.test;import java.lang.reflect.Method;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.Hibernat

sping hibernate 事务-spring注解方式事务控制没有回滚

问题描述 spring注解方式事务控制没有回滚 项目中使用到了hibernate以及spring事务控制,在service层增加事务控制但是遇到异常没有回滚. 代码如下: 配置文件 <?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springfr

求救:spring+struts2+hibernate事务

问题描述 用spring+struts2+hibernate做了一个项目,一切正常,只是睡一晚上起来再打开就出现以下异常,然后刷新一下就又没事了.什么原因?望大位大虾指点迷津.exceptionjavax.servlet.ServletException:org.springframework.transaction.CannotCreateTransactionException:CouldnotopenHibernateSessionfortransaction;nestedexceptio

关于hibernate 事务提交的问题

问题描述 近期做项目,遇到了一个问题,用hibernate做数据持久化,可是有的业务逻辑要求是这样的,三张表是独立的.如何将三张表的增删改放到一个事务里面?比如:我先保存一个新的人员信息.然后再查询出保存完之后的人员信息同时要update另外一张表里面.问题就是:session.save(obj) 之后再查询的话.这个对象还是游离状态.会查询不到.所以就不能后面的update操作 问题补充:fmjsjx 写道 解决方案 引用session.save() 一个对象的时候,没有commit() 事务

Hibernate事务问题

问题描述 如果在dao执行多条语句一个事务为什么不行而写在业务逻辑就可以 解决方案 解决方案二:事务的控制在SPRING还是HIBERNATE.如果是SPRING控制了,那HIBERNATE的DAO就不能再控制事务.通常也都是在业务层用SPRING控制事务最好是有代码放出来溜溜解决方案三:顶楼上的,如果集成了Spring,事务的管理已经已经由Spring接管了.解决方案四:我也顶娄山的解决方案五:顶解决方案六:spring接管了hibernate的事务管理!使hibernate的这边不用再花太多

hibernate事务的疑惑

问题描述 在项目的代码中发现一行这样的代码this.getSession().beginTransaction().commit(); 这个类是我们项目经理维护的.我是看不出这行代码有什么用. 请问这么写有什么特殊的作用吗? 解决方案 这应该是一行待添加的代码,相当于TODO类的代码.表示在此处处理事务,但是现在由于没有要处理的,先空在那.以后扩展时在拆分开:this.getSession().beginTransaction()...............commit();

hibernate 事务

问题描述 public ActivityRemark addRemark(String userId, String activityId, int remark, String remarkContent){ActivityRemark ar = new ActivityRemark();ar.setUserId(userId);ar.setActivityId(activityId);ar.setRemark(remark);ar.setRemarkContent(remarkContent