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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题