`
wangtong40
  • 浏览: 248733 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate的Java SE事务

    博客分类:
  • ORM
阅读更多

1、在Hibernate的配置文件中添加以下内容:

 

<property name="hibernate.transaction.factory_class">
	org.hibernate.transaction.JDBCTransactionFactory
 </property>

 2、Java代码如下:

Session session = null;
		Transaction tx = null;
		try {
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			tx.setTimeout(5);//允许事务运行是时间
			session.saveOrUpdateCopy(object);
			tx.commit();
		} catch (HibernateException e) {
			try {
				if (tx != null) {
					tx.rollback();
				}
			} catch (HibernateException ex) {
				ex.printStackTrace();
				throw new InfrastructureException(ex);
			}
			e.printStackTrace();
			throw new InfrastructureException(e);
		} finally {
			try {
				session.close();
			} catch (HibernateException e) {
				e.printStackTrace();
				throw new InfrastructureException(e);
			}
		}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics