码迷,mamicode.com
首页 > 其他好文 > 详细

事务管理

时间:2017-02-07 22:46:38      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:value   his   style   ack   text   druid   rollback   import   get   

**事务处理**
1.编程式事务

//    编程式事务
    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-pool.xml");
        DruidDataSource ds = (DruidDataSource) context.getBean("dataSource");
        
        Connection con = null;
        Statement stmt = null;
        ResultSet result = null;
    
        try {
            con = ds.getConnection();
            stmt = con.createStatement();
            //关闭自动提交
            con.setAutoCommit(false);
            
            String insertSql = "insert into eac_core_biz "
                    + "(id, biz_system_id, biz_system_name, status_code, export, created_by, create_time, "
                    + "updated_by, update_time) values (14, ‘Test2‘, ‘帝景豪庭‘, 2, null, null, ‘2016-12-13 17:29:44‘, "
                    + "null, ‘2016-12-13 17:29:44‘)";
             //插入数据操作
            stmt.executeUpdate(insertSql);
            
            //模拟异常出现
            Assert.isTrue(false,"事务测试,模拟异常");
            
             //更新数据操作
            stmt.executeUpdate("update eac_core_biz set biz_system_id=‘efc‘ where id=14");
            //事务提交
            con.commit(); 
        } catch (Exception e) {
             //事务回滚
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            } 
        }finally{
            closeResultSet(result);
            closeStatement(stmt);
            closeConnection(con);
        }
    }

 

2.注解式事务

<!-- 定义数据源 -->
    <bean id="dataSource"     
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">     
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://localhost:3306/yun?characterEncoding=UTF8" />  
        <property name="username" value="root" />     
        <property name="password" value="centos" />  
    </bean> 
    
     <!-- 定义JdbcTemplate的Bean -->  
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"  
            p:dataSource-ref="dataSource">  
    </bean>  
    
    <!-- 配置事务管理器 -->  
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"  
            p:dataSource-ref="dataSource">  
    </bean>
    
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="txManager" /> 
    
    <bean id="userDao" class="com.guo.dao.impl.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

 

接口 IUserDao

package com.guo.dao;

import com.guo.entity.User;

public interface IUserDao {
    void insertUser(User user);
}

 

实现 UserDaoImpl 

package com.guo.dao.impl;

import java.sql.Types;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.guo.dao.IUserDao;
import com.guo.entity.User;

@Transactional
public class UserDaoImpl implements IUserDao {
    
    Logger LOGGER = Logger.getLogger(getClass());
    
    private JdbcTemplate jdbcTemplate;
    
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void insertUser(User user) {
        
        String insertSql = "insert into student (ID, NAME, AGE) values (?,?,?)";
        Object[] params = new Object[] { user.getId(),user.getName(),user.getAge() };
        int[] types = new int[] { Types.INTEGER, Types.VARCHAR,Types.INTEGER };
        int number = jdbcTemplate.update(insertSql, params, types);
        LOGGER.info("执行语句{insert into student (ID, NAME, AGE) values (?,?,?)},生效"+number+"条。");
        
        //模拟异常出现
//        Assert.isTrue(false,"事务测试,模拟异常");
    }

}

 

@Transactional 注解可用于实现类上,也可以放在具体的方法上。

 

3.spring aop 拦截器事务

 

事务管理

标签:value   his   style   ack   text   druid   rollback   import   get   

原文地址:http://www.cnblogs.com/yun965861480/p/6376090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!