<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<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/db_shop?useUnicode=true&amp;characterEncoding=gb2312"/>
    <property name="username" value="root"/>
    <property name="password" value="ydm"/>
     <property name="initialSize" value="1"/>
   <property name="maxActive" value="500"/>
   <property name="maxIdle" value="2"/>
 <property name="minIdle" value="1"/>
  </bean>
 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/> 
<bean id="link" class="com.spring.LinkServiceImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
这个是我配置文件的代码public void delete(Integer linkid) {
String sql="delete from tb_links where id=?";
Object[] parms=new Object[]{linkid};
int []types=new int[]{Types.INTEGER};
jdbcTemplate.update(sql, parms, types);
    throw new RuntimeException("运行期以外");这个是Dao代码..private static LinksService linkservice; @BeforeClass
public static void setUpBeforeClass() throws Exception {
try{
ApplicationContext ctx=new ClassPathXmlApplicationContext("Bean.xml");
linkservice=(LinksService)ctx.getBean("link");
}
catch(RuntimeException e)
{
e.printStackTrace();
}
@Test public void linkTest()
{
       linkservice.delete(9);
}
这是测试代码...本来抛出运行期异常spring应该默认会回滚事务的....可我这个没有回滚..数据库的数据还是被删掉了..我用是MySQL5.0..搞了半天还是不清楚哪里出了问题...大家帮忙看看

解决方案 »

  1.   

    jdbcTemplate.update(sql, parms, types);
    throw new RuntimeException("运行期以外");故意抛异常测试?假如这样:
    1,插入
    2,查询
    2失败的时候有必要回滚1吗?你这个代码好像没必要回滚吧?
    try{
     update...
     commit();
    }catch(Exception e){
     rollback();
    }
      

  2.   

      我记得当时我遇到这个问题的时候,是因为数据库的表,不支持事务!如果mysql不支持存储引擎,它将以MyISAM表创建表,这是非事务性表。 一般修改成InnoDB.
    可使用下述语句之一检查表的标类型: 
    SHOW TABLE STATUS LIKE 'tbl_name';
    SHOW CREATE TABLE tbl_name;
      使用下述语句,可检查mysqld服务器支持的存储引擎: 
    SHOW ENGINES;
        也可以使用下述语句,检查与你感兴趣的存储引擎有关的变量值: SHOW VARIABLES LIKE 'have_%';  例如,要想确定InnoDB存储引擎是否可用,可检查have_innodb变量的值。   
      

  3.   

    配置文件加入: 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  类加入:
    @Transactional
    public void delete(Integer linkid) {
    }这样应该可以了吧!!
      

  4.   


    MyISAM表创建表,这是非事务性表 ...确实是MyISAM...怎么改成InnoDB呢