spring的事务注解没有起作用

xuchuandi 2016-02-16 10:04:58
先看xml配置

<context:component-scan base-package="com.atom.bean.tx"></context:component-scan>

<context:property-placeholder location="classpath:db.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
</bean>

<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>

<tx:annotation-driven transaction-manager="transactionManager" />


类都在com.atom.bean.tx包下
看两个dao的实现类


@Repository
public class BookDaoImpl implements BookDao {

@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void updateStore(Integer bookId) {
String sql = "UPDATE book SET store = store-1 where bookid=?";
jdbcTemplate.update(sql, bookId);
}

}


@Repository
public class UserAccountDaoImpl implements UserAccountDao {

@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public void updateAccount(String userName , Integer account) {

String sql = "UPDATE useraccount SET account = account - ? where username = ?";
jdbcTemplate.update(sql, account , userName);
}

}


server的实现类
throw new RuntimeException("余额不足");这里会抛出异常。
但是dao.updateStore(bookid);依然更新更新成功了



@Service
public class BuyServiceImpl implements BuyService {

@Transactional
public void buy(String username, Integer bookid) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-tx.xml");
BookDao dao = ctx.getBean(BookDao.class);
int store = dao.findStore(bookid);
if(store <= 0)
throw new RuntimeException("库存不足");

dao.updateStore(bookid);


UserAccountDao accountDao = ctx.getBean(UserAccountDao.class);
Integer accountInteger = accountDao.findAccount(username);
if(accountInteger < 100)
throw new RuntimeException("余额不足");

accountDao.updateAccount(username, 100);

}

}


测试类junit3

public void testBuyService(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-tx.xml");
BuyService service = ctx.getBean(BuyService.class);
System.out.println(AopUtils.isAopProxy(service));
service.buy("aa", 1);
}
...全文
176 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
楼主请参考Spring 使用注解方式进行事务管理 http://logs.com/younggun/archive/
  • 打赏
  • 举报
回复
http://blog.csdn.net/chunqiuwei/article/details/16115135
xuchuandi 2016-02-17
  • 打赏
  • 举报
回复
引用 4 楼 baohuan_love 的回复:
楼主请参考Spring 使用注解方式进行事务管理 http://logs.com/younggun/archive/
提供法人URL无法访问
xuchuandi 2016-02-17
  • 打赏
  • 举报
回复
提供的url无法访问
xuchuandi 2016-02-16
  • 打赏
  • 举报
回复
后来我把日志输出,发现两次init 连接池,是哪里配置有问题吗?

2016-02-16 22:32:41,806 INFO [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge14h9e1t2bvdv6fl1nz|6ef888f6, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge14h9e1t2bvdv6fl1nz|6ef888f6, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql:///test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 20, maxStatementsPerConnection -> 5, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
2016-02-16 22:32:42,247 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6f45df59: startup date [Tue Feb 16 22:32:42 CST 2016]; root of context hierarchy
2016-02-16 22:32:42,248 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [bean-tx.xml]
2016-02-16 22:32:42,365 INFO [org.springframework.context.support.PropertySourcesPlaceholderConfigurer] - Loading properties file from class path resource [db.properties]
2016-02-16 22:32:42,502 INFO [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge14h9e1t2bvdv6fl1nz|757acd7b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge14h9e1t2bvdv6fl1nz|757acd7b, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, jdbcUrl -> jdbc:mysql:///test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 20, maxStatementsPerConnection -> 5, minPoolSize -> 5, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]

xuchuandi 2016-02-16
  • 打赏
  • 举报
回复
将autocommit未改0也不可以
xuchuandi 2016-02-16
  • 打赏
  • 举报
回复
使用数据库mysql,两个表都是使用InnoDB。 +--------------+ | @@autocommit | +--------------+ | 1 | +--------------+
第1章:对Spring框架进行宏观性的概述,力图使读者建立起对Spring整体性的认识。   第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。   第3章:讲解Spring IoC容器的知识,通过具体的实例详细地讲解IoC概念。同时,对Spring框架的三个最重要的框架级接口进行了剖析,并对Bean的生命周期进行讲解。   第4章:讲解如何在Spring配置文件中使用Spring 3.0的Schema格式配置Bean的内容,并对各个配置项的意义进行了深入的说明。   第5章:对Spring容器进行解构,从内部探究Spring容器的体系结构和运行流程。此外,我们还将对Spring容器一些高级主题进行深入的阐述。   第6章:我们从Spring AOP的底层实现技术入手,一步步深入到Spring AOP的内核中,分析它的底层结构和具体实现。   第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。   第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等内容。   第9章:介绍了Spring事务管理的工作机制,通过XML、注解等方式进行事务管理配置,同时还讲解了JTA事务配置知识。   第10章:对实际应用中Spring事务管理各种疑难问题进行透彻的剖析,让读者对Spring事务管理不再有云遮雾罩的感觉。   第11章:讲解了如何使用Spring JDBC进行数据访问操作,我们还重点讲述了LOB字段处理、主键产生和获取等难点知识。   第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到ORM框架的混用和DAO层设计的知识。   第13章:本章重点对在Spring中如何使用Quartz进行任务调度进行了讲解,同时还涉及了使用JDK Timer和JDK 5.0执行器的知识。   第14章:介绍Spring 3.0新增的OXM模块,同时对XML技术进行了整体的了解。   第15章:对Spring MVC框架进行详细介绍,对REST风格编程方式进行重点讲解,同时还对Spring 3.0的校验和格式化框架如果和Spring MVC整合进行讲解。   第16章:有别于一般书籍的单元测试内容,本书以当前最具实战的JUnit4+Unitils+ Mockito复合测试框架对如何测试数据库、Web的应用进行了深入的讲解。   第17章:以一个实际的项目为蓝本,带领读者从项目需求分析、项目设计、代码开发、单元测试直到应用部署经历整个实际项目的整体开发过程。

67,550

社区成员

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

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