当前位置: 代码迷 >> J2EE >> spring3.0 mysql数据库不回滚!解决方法
  详细解决方案

spring3.0 mysql数据库不回滚!解决方法

热度:101   发布时间:2016-04-17 22:58:39.0
spring3.0 mysql数据库不回滚!
配置
<?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: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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"  value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
 
</bean>

<!-- 定义spring jdbc模板类Bean -->
<bean id="jdbcTemplate"
        class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
        lazy-init="false" autowire="default" >
<property name="dataSource" ref="ds" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"/>
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

下面为访问数据库代码
package com.river.test.dao;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Repository 
public class IndexDaoImpl implements  IndexDao {


@Autowired
private JdbcTemplate jdbcTemplate;

@Transactional
public void TestInsertDB() {
String addStr = "INSERT INTO t1 ( NAME ) VALUES ('xxxxx');";
this.jdbcTemplate.update(addStr);
throw new RuntimeException("A runtime exception");
}

}

以下为controller代码
package com.river.test.controller;


import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.river.test.dao.IndexDao;


@Controller
public class IndexController {

@Autowired
private IndexDao indexDao;

@RequestMapping("/helloworld")  
    public void helloworld(HttpServletRequest request,
HttpServletResponse response) throws SQLException, IOException {  
        // return "success"; 
response.setContentType("text/html;charset=utf-8");
indexDao.TestInsertDB();
response.getWriter().print("aaaa");
      //  return "helloWorld";  
    }  


}

执行后,数据依然被插入数据库,不会回滚,有大师帮忙吗?多谢!

------解决思路----------------------
把“<tx:annotation-driven transaction-manager="transactionManager" />”这段代码加到spring-mvc.xml里面试试。
------解决思路----------------------
确定你代码没问题的话  看下你建的那个数据库   MySQL数据库默认的存储引擎类型是MyISAM,这种存储引擎类型不支持事务处理。在MySQL中,只有InnoDB存储引擎类型的数据表才能支持事务处理。
------解决思路----------------------
引用:
dokia12,我把那句话移到springmvc确实可以了,可以会滚了,,原因是什么啊?

因为spring启动的时候最先扫描的springmvc.xml,扫描这个文件的时候已经生成了service的bean,而springmvc.xml里面没有关于开启事务控制的那行代码,所以生成的service bean都没有事务代理。
  相关解决方案