Spring Hibernate Transaction示例

JDBCDAO.java

package com.bf;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import java.util.List; 
import java.util.Date;
import java.util.Iterator; 
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class JDBCDAO implements SpitterDAO {
    private static String GET_SQL = "select code_item from trans_schedule where ID_NO = ?";

    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    public SpitterObj getSpitter(int ljdm) {
        return jdbcTemplate.queryForObject(GET_SQL, new RowMapper<SpitterObj>() {
            public SpitterObj mapRow(ResultSet rs, int rowNum) throws SQLException {
                SpitterObj obj = new SpitterObj();
                obj.setLOT(rs.getString(1));
                return obj;
            }
        }, ljdm);
    }
    
    private static SessionFactory factory; 
    
    public JDBCDAO() {
    } 
        
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.factory = sessionFactory;
    }
    
    public void SaveEvent(){
        factory.getCurrentSession().persist(new Event( "Our very first event!", new Date()));
       throw new RuntimeException("asdfsafd");  
    }
}
View Code

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:" />
        <property name="username" value="" />
        <property name="password" value="" />
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource" />
    </bean>
    <bean id="dao" class="com.bf.JDBCDAO" autowire="byName">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
         <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="Audience" class="com.bf.Audience" autowire="byName">
    </bean>
     
       <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>Event.hbm.xml</value>
            </list>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <aop:config>
        <aop:pointcut id="bizServiceMethod" expression="execution(* com.bf.SpitterDAO.SaveEvent(..))" />
        <aop:advisor pointcut-ref="bizServiceMethod" advice-ref="bizAdvice" />
    </aop:config>
    
    <tx:advice id="bizAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="Save*" propagation="REQUIRED" rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
</beans>
View Code。

Spring并不直接管理事务,而是提供了多种事务管理器,它们将事务管理的职责委托给JTA(Java Transaction API)或其它持久化机制所提供的平台相关的事务实现。如上面的HibernateTransactionManager,用于Hibernate进行持久化。

 

Application.java

package com.bf;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import java.util.List; 
import java.util.Date;
import java.util.Iterator; 
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@SuppressWarnings("unused")
public class Application {

    
	  public static void main(String[] args) {
	      try{
	          ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	          
	          SpitterDAO dao = (SpitterDAO)context.getBean("dao");
	          
	          dao.SaveEvent();
	      }catch (Throwable ex) { 
	         throw new ExceptionInInitializerError(ex); 
	      }
	  }
	}

 

事务基本特性 ACID

原子性(Atomic):      事务中所有操作要么全部完成,要么全部不完成。

一致性(Consistent): 一旦事务完成(不管是成功还是失败),系统必须确保它所建模的业务处于一致的状态。现实的数据不应该被损坏。

隔离性(Isolated):    事务允许多个用户对相同的数据进行操作,每个用户的操作不会与其它用户纠缠在一起。因此,事务应该被隔离,避免发生同步读写相同数据的事情。

持久性(Durable):   一旦事务完成,事务的结果应该持久化。

原子性与 隔离性确保了一致性。

 

声明式事务属性

在Spring中,声明式事务是通过事务属性来定义的。

上面xml中定义的propagation="REQUIRED"表示当前方法必须运行在事务中,其它的值为REQUIRED_NEW等。它定义事务的传播行为。

声明式事务的第二个维度是隔离级别,隔离级别定义了一个事务可能受其它并发事务的影响程度。并发虽然是必须的,但可能会导致如下问题

 

 

 

另一个版本:https://spring.io/guides/gs/managing-transactions/

posted @ 2016-03-10 17:24  Season2009  阅读(194)  评论(0编辑  收藏  举报