yangleiol 2010-08-05 09:39
浏览 604
已采纳

spring 注解事务不起作用(太奇怪了)

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


 

  • 写回答

7条回答 默认 最新

  • oyj7454103 2010-08-07 09:01
    关注

    程序已经帮你调试通过了! 请联系我 接受!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!