CopyOfUserServiceImp.java
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; } }
applicationContext.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: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,插入数据。太奇怪了。不知道哪里有点问题,我自己也折腾了好长时间。