spring中事务管理使用@Transactional无法实现回滚,

shuangbofu 2017-01-13 01:14:01
Dao层:

package cn.BlackHumour.b_anno;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
* dao实现,使用Spring对jdbc支持功能
*
*/
@Repository
public class DeptDao {


@Resource
private JdbcTemplate jdbcTemplate;

public void save(Dept dept) {
String sql = "insert into t_dept (deptName) values(?);";
jdbcTemplate.update(sql,dept.getDeptName());
}
}






Service层:

package cn.BlackHumour.b_anno;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
*
*
*/
@Service
public class DeptService {

@Resource
private DeptDao deptDao;

/**
* 事务控制
*/
@Transactional()
public void save(Dept dept) {
deptDao.save(dept); // 第一次
int i = 1/0;
deptDao.save(dept); // 第二次
}
}






测试:
package cn.BlackHumour.b_anno;

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


public class App {

@Test
public void testApp() throws Exception {
// 容器对象
ApplicationContext aContext = new ClassPathXmlApplicationContext("cn/BlackHumour/b_anno/bean.xml");

// 模拟数据
Dept dept = new Dept();
dept.setDeptName("测试");

DeptService deptService = (DeptService) aContext.getBean("deptService");
deptService.save(dept);
}
}







bean.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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<!-- 1. 数据源对象:C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>

<!-- 2. 配置JdbcTamplate工具类实例 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理器类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.BlackHumour.b_anno"></context:component-scan>

<!-- 注解方式实现事务:指定注解方式实现事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
...全文
70 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcsflilong 2017-01-13
  • 打赏
  • 举报
回复
引用 3 楼 BlackHumour_bo 的回复:
[quote=引用 1 楼 bcsflilong 的回复:] 应该是你的注解处理器没打开吧 <!-- 打�注解处理器 --> <context:annotation-config />
我已经使用了<context:component-scan/>开启了注解扫描,不需要用<context:annotation-config/>这个了[/quote]你添加上试试
shuangbofu 2017-01-13
  • 打赏
  • 举报
回复
引用 2 楼 shzy1988 的回复:
看看你的mysql表使用的是不是innodb引擎。只有innodb才支持事务
对的,我看了下,那个表是引擎是MyISAM,只有innodb才支持外键和事务,怪不得之前查看外键也有错,谢了!
shuangbofu 2017-01-13
  • 打赏
  • 举报
回复
引用 1 楼 bcsflilong 的回复:
应该是你的注解处理器没打开吧 <!-- 打�注解处理器 --> <context:annotation-config />
我已经使用了<context:component-scan/>开启了注解扫描,不需要用<context:annotation-config/>这个了
双子叶 2017-01-13
  • 打赏
  • 举报
回复
看看你的mysql表使用的是不是innodb引擎。只有innodb才支持事务
bcsflilong 2017-01-13
  • 打赏
  • 举报
回复
应该是你的注解处理器没打开吧 <!-- 打�注解处理器 --> <context:annotation-config />

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧