编程式事务和声明式事务
一、编程式事务:
下面只帖关于事务的两个关键代码:
hibernate.cfg.xml
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/bjsxt/usermgr/model/Log.hbm.xml" />
<mapping resource="com/bjsxt/usermgr/model/User.hbm.xml" />
</session-factory>
package com.bjsxt.usermgr.manager;
import com.bjsxt.usermgr.model.Log;
import com.bjsxt.usermgr.util.HibernateUtils;
public class LogManagerImpl implements LogManager {
public void addLog(Log log) {
HibernateUtils.getSessionFactory().getCurrentSession().save(log);
}
}
package com.bjsxt.usermgr.manager;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.bjsxt.usermgr.model.Log;
import com.bjsxt.usermgr.model.User;
import com.bjsxt.usermgr.util.HibernateUtils;
public class UserManagerImpl implements UserManager {
public void addUser(User user) {
Session session = null;
try {
session = HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(user);
Log log = new Log();
log.setType("安全日志");
log.setDetail("***进入系统!");
log.setTime(new Date());
LogManager logManager = new LogManagerImpl();
logManager.addLog(log);
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
} //finally {
// session.close();
//}
}
}
注:1.getCurrentSession()与getSession()的区别
※ 采用getCurrentSession()创建的Session会绑定到当前线程中,而采用openSession()创建的session则不会
※ 采用getCurrentSession()创建的Session在commit或rollback时会自动关闭,而采用openSession创建的session()必须手动关闭
2.采用getCurrentSession()创建的session需要在hibernate.cfg.xml中加入如下配置:
※采用本地事务:
<property name="hibernate.current_session_context_class">thread</property>
※ 采用jta事务:
<property name="hibernate.current_session_context_class">jta</property>
二、声明式事务
※声明式事务配置
● 配置SessionFactory
● 配置事务管理器
● 事务的传播特性
● 那些类那些方法使用事务
※编写业务逻辑方法
● 继承HibernateDaoSupport类,使用HibernateTemplate来持久化,HibernateTemplate类是对Hibernate Session的轻量级封装
● 默认情况下运行期异常才会回滚(包括继承了RuntimeException的子类),普通异常是不会回滚的
● 编写业务逻辑方法时最好将异常一直往上抛,在表示层(struts)处理
● 关于业务边界的设置,通常设置在业务层,不要添加到DAO上。
ApplicationContext.bean.xml
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="deploy*" propagation="REQUIRED"/>
<tx:method name="submit*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution (* com.bjsxt.usermgr.manager.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>
ApplicationContext.common.xml
<bean id="logManager" class="com.bjsxt.usermgr.manager.LogManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userManager" class="com.bjsxt.usermgr.manager.UserManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
<property name="logManager" ref="logManager"></property>
</bean>
</beans>
LogManagerImpl.java
package com.bjsxt.usermgr.manager;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bjsxt.usermgr.model.Log;
public class LogManagerImpl extends HibernateDaoSupport implements LogManager {
public void addLog(Log log) {
this.getHibernateTemplate().save(log);
}
}
UserManagerImpl.java
package com.bjsxt.usermgr.manager;
import java.util.Date;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bjsxt.usermgr.model.Log;
import com.bjsxt.usermgr.model.User;
public class UserManagerImpl extends HibernateDaoSupport implements UserManager {
private LogManager logManager;
public void setLogManager(LogManager logManager) {
this.logManager = logManager;
}
public void addUser(User user) {
this.getHibernateTemplate().save(user);
Log log = new Log();
log.setType("安全日志");
log.setDetail("***进入系统!");
log.setTime(new Date());
logManager.addLog(log);
}
}