spring 2.5 第三部分 事务---笔记

 

1)<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>

    <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>

    <property name="username" value="root"/>

    <property name="password" value="123456"/>

     property name="initialSize" value="1"/>

<!-- 连接池的最大值 -->

<property name="maxActive" value="500"/>

<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->

<property name="maxIdle" value="2"/>

<!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->

<property name="minIdle" value="1"/>

 

  </bean>

  从2.0开始就可以采用注解方式配置数据源

  commons-dbcp.jar

  commons-loging.jar

  commons-pool.jar

 

  2)

  <beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

   <property name="dataSource" ref="dataSource"/>

  </bean>

<tx:annotation-driven transaction-manager="txManager"/> <!--  用于注释方式配置 -->

</beans>

 

3)

JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);

 

jdbcTemplate.update("insert into person(name) values(?)" ,new Object[]{persion.getName()},new int[]{java.sql.Types.VARCHAR});

 

jdbcTemplate.update("update person set name=? where id=?",new Object[]{person.getname()},new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});

jdbcTemplate.update("delete from person  where id=?",new Object[]{personid},new int[]{java.sql.Types.INTEGER});

 

jdbcTemplate.queryForObject(String,sql,Object[] args,int[] argTypes,RowMapper rowMapper) Object -JdbcTemplate

 

return (Person)jdbcTemplate.queryForObject("select * from person where id=?",new Object[]{personid},new int[]{java.sql.Types.INTEGER},

new PersonRowMapper());

 

return (List<Person>)jdbcTemplate.query("select * from person ",new PersonRowMapper());

 

 

public class PersonRowMapper implements RowMapper{

 

public Object mapRow(ResultSet rs,int index) throws SQLException{

Person person=new Person(rs.getString("name"));

person.setId(rs.getInt("id"));

person.getName(rs.getString("name"));

return person;

}

}

 

 

4)在业务bean上加上

  @Transactional

  这样事务是基于方法的

 

5)连接的用户名,密码等保存在jdbc.properties文件中,放在类路径下

<context:property-placeholder location="classpath:jdbc.properties"/>属性占位符

<property name="driverClassName" value="${driverClassName}" />

 

6) 事务的传播属性

 默认对于RuntimeException (uncheckedException)事务会回滚

 对于checked Excpetion 事务并不会回滚

 

  @Transactional(rollbackFor=Exception.class}

public void delete(Integer personid)throws Exception{

jdbcTemplate.update("delete from person  where id=?",new Object[]{personid},new int[]{java.sql.Types.INTEGER});

throw new Exception("例外");

}

 

关闭方法的事务,在方法前要加上

@Transactional(propagation=Propagation.NOT_SUPPORTED)

默认是REQUIRED

 

7)REQUIRED:业务方法需要在一个事务中运行。如果方法运行时,已经处在一个事务中,那么加入到该事务,

        否则为自己创建一个新的事务。

NOT_SUPPORTED:声明方法不需要事务。如果方法没有关联到一个事务,容器不会为它开启事务。

      如果方法在一个事务中被调用,该事务会被挂起,在方法调用结束后,原先的事务便会恢复执行。

REQUIRESNEW:属性表明不管是否存在事务,业务方法总会为自己发起一个新的事务。如果方法已经运行在一个事务中,

      则原有事务会被挂起,新的事务会被创建,直到方法执行结束,新事务才算结束,原先的事务才会恢复执行。

MANDATORY:该属性指定业务方法只能在一个已经存在的事务中执行,业务方法不能发起自己的事务。

     如果业务方法在没有事务的环境下调用,容器就会抛出例外。

SUPPORTS:这一事务属性表明,如果业务方法在某个事务范围内被调用,则方法成为该事务的一部分。

     如果业务方法在事务范围外被调用,则方法在没有事务的环境下执行。

Never:指定业务方法绝对不能在事务范围内执行。如果业务方法在某个事务中执行,容器会抛出例外,

   只有业务方法没有关联到任何事务,才能正常执行。

NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务, 则按REQUIRED属性执行.

       它使用了一个单独的事务, 这个事务拥有多个可以回滚的保存点。内部事务的回滚不会对外部事务造成影响。

       它只对DataSourceTransactionManager事务管理器起效

 

8)关于NESTED方式 ,它会自动创建保存点(SavePoint)---自从jdbc3的有的新特性

 

@Resource OtherService otherService;  

public void xxx(){

stmt.executeUpdate("update person set name='888' where id=1");

otherService.update(); //otherService的update方法的事务传播属性为NESTED

stmt.executeUpdate("delte from person where id=9");

}

 

 

 

 

Connection conn = null;

try {

    conn.setAutoCommit(false);

    Statement stmt = conn.createStatement();

    stmt.executeUpdate("update person set name='888' where id=1");

    Savepoint savepoint = conn.setSavepoint();

    try{   

            conn.createStatement().executeUpdate("update person set name='222' where sid=2");

    }catch(Exception ex){

            conn.rollback(savepoint);    

     }

      stmt.executeUpdate("delete from person where id=9");

      conn.commit();

       stmt.close();

    } catch (Exception e) {

         conn.rollback();

     }finally{

               try {

   if(null!=conn && !conn.isClosed()) conn.close();

                } catch (SQLException e) { e.printStackTrace(); }

     }

}

 

9)

@Transactional(readOnly=true)

@Transactional(timeout=30)

数据库系统提供了四种事务隔离级别供用户选择。不同的隔离级别采用不同的锁类型来实现,

在四种隔离级别中,Serializable的隔离级别最高,Read Uncommited的隔离级别最低。大多数据库默认的隔离级别为Read Commited,

如SqlServer,当然也有少部分数据库默认的隔离级别为Repeatable Read ,如Mysql

Read Uncommited:读未提交数据(会出现脏读,不可重复读和幻读)。

Read Commited:读已提交数据(会出现不可重复读和幻读)

Repeatable Read:可重复读(会出现幻读) ,可以采用快照技术实现

Serializable:串行化

 

脏读:一个事务读取到另一事务未提交的更新新据。

不可重复读:在同一事务中,多次读取同一数据返回的结果有所不同。

           换句话说就是,后续读取可以读到另一事务已提交的更新数据。

           相反,“可重复读”在同一事务中多次读取数据时,能够保证所读数据一样,

          也就是,后续读取不能读到另一事务已提交的更新数据。

幻读:一个事务读取到另一事务已提交的insert数据。

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值