问题描述
如果我没用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类