package com.zjhc; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.springframework.transaction.annotation.Transactional; public class CopyOfUserServiceImp implements UserService{ private DataSource dataSource; @Transactional(readOnly=true) public void addUser() { try { Connection cn=dataSource.getConnection(); Statement st=cn.createStatement(); st.executeUpdate("insert into user(name,password) values('y','1')"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //throw new UnsupportedOperationException(); } public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } }
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> <import resource="classpath:spring-generated-dao-context.xml"/> <import resource="classpath:spring-dao-context.xml"/> <import resource="classpath:spring-generated-security-context.xml"/> <import resource="classpath:spring-security-context.xml"/> <import resource="classpath:spring-generated-service-context.xml"/> <import resource="classpath:spring-service-context.xml"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/spring"/> <property name="username" value="root"/> <property name="password" value="19881204"/> </bean> <bean id="CopyOfUserServiceImp" class="com.zjhc.CopyOfUserServiceImp"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
测试类:
package com.zjhc; import org.springframework.context.support.ClassPathXmlApplicationContext; public class test { public static void main(String args[]) { ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService usi = (UserService)factory.getBean("CopyOfUserServiceImp"); usi.addUser(); } }
Uservice是CopyOfUserServiceImp的接口。
我刚开始用xml配置事务的时候(也就是 <tx:advice> <aop:config>这种形式),没有问题,会出现事务为只读无法插入数据。
但当我改成,在add方法上用注解声明
@Transactional(readOnly=true)
发现仍然可以执行insert,插入数据。太奇怪了。不知道哪里有点问题,我自己也折腾了好长时间。
程序已经帮你调试通过了! 请联系我 接受!
类上加@Service
不对 这个 类也需要
@Transactional
public class CopyOfUserServiceImp implements UserService{
试试 :wink:
吧你的项目 打包给我发一下
我本地调试下! 那个下载不了!
我QQ : 252821719
事务你只是选了只读了,只读只是一般用于查询记录的时候才使用的,你尝试一下以下这样的方法
@Transactional(propagation = Propagation.REQUIRED)
public void addUser() {
try {
Connection cn=dataSource.getConnection();
Statement st=cn.createStatement();
int i =0;
i = 5/0;
st.executeUpdate("insert into user(name,password) values('y','1')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//throw new UnsupportedOperationException();
}
试试出错的时候时候有回滚,如果有回滚的话而且数据没有插入的话那证明事务起作用了
1.使用DataSourceUtils.getConnection(dataSource)来获得数据库连接。spring把开启事务的连接放在线程变量里了,dataSource.getConnection()重新取新的连接当然不行了。
2.mysql使用了哪个模式,可能数据库就不支持事务。检查log输出看看事务管理是否执行了。