事务:
它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性。就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操 作过程中机器突然出故障的情况,此时,事务就必须确保出故障前对账户的操作不生效,就像用户刚才完全没有使用过取款机一样,以保证用户和银行的利益都不受损失。
原子性:组成事务处理的语句形成了一个逻辑单元,不能只执行其中的一部分
一致性:在事务处理执行之前和之后,数据是一致的。
隔离性:一个事务处理对另一个事务处理没有影响。
持续性:当事务处理成功执行到结束的时候,其效果在数据库中被永久纪录下来。
编程式事务:
在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择。用过 hibernate 的人都知道,我们需要在代码中显式调用beginTransaction()、commit()、rollback()等事务管理相关的方法,这就是编程式事务管理。
比如:
package com.cpic.wechat.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.cpic.weixin.util.ServiceUtil;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
// private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal
threadLocal = new ThreadLocal
();
// private static Configuration configuration = new AnnotationConfiguration();
private static org.hibernate.SessionFactory sessionFactory;
// private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
// configuration.configure(configFile);
// sessionFactory = configuration.buildSessionFactory();
sessionFactory = (SessionFactory) ServiceUtil.getService("sessionFactory");
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the
SessionFactory
if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
// configuration.configure(configFile);
// sessionFactory = configuration.buildSessionFactory();
sessionFactory = (SessionFactory) ServiceUtil.getService("sessionFactory");
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
/* public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}*/
/**
* return hibernate configuration
*
*/
/*public static Configuration getConfiguration() {
return configuration;
}*/
}
public class BaseDao
{
public Session getSession() {
return HibernateSessionFactory.getSession();
}
public void create(T object) {
Session session =getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(object);
// session.persist(object);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateSessionFactory.closeSession() ;
}
}
声明式事务:
HibernateTransactionManager把sessionFactory交个spring管理,JpaTransactionManager把EntityManagerFactory交给spring管理,他们的核心是aop(通过反射的方式进行动态代理)
org.hibernate.cfg.ImprovedNamingStrategy
false