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);
}
}