教主无忌 2008-11-15 19:17
浏览 235
已采纳

Spring2.5声明事务<tx:><aop:>方式不生效

问题如题,方式试了很久就是不行,改成TransactionProxyFactoryBean方式就行,我把代码贴出来,请大家帮忙看看哪有问题。
[b]applicationContext.xml[/b][code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">





class="org.springframework.jdbc.datasource.DriverManagerDataSource">
value="com.mysql.jdbc.Driver" />
value="jdbc:mysql://localhost:3306/test" />


<bean id="managerImpl" class="transaction.ManagerImpl">
    <property name="fooDAO">
        <ref bean="fooDAO" />
    </property>
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">

    <tx:attributes>
        <!-- all methods starting with 'get' are read-only -->
        <tx:method name="get*" read-only="true" />
        <!-- other methods use the default transaction settings (see below) -->
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>


<aop:config>
    <aop:advisor id="managerTx" advice-ref="txAdvice"
        pointcut="execution(* transaction.ManagerImpl.*(..))" />    
</aop:config>

<bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- 这样配置事务能生效
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="baseTxProxy"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
    lazy-init="true">
    <property name="transactionManager">
    <ref bean="transactionManager" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    <bean id="manager" parent="baseTxProxy">
    <property name="target">
    <bean class="transaction.Manager">
    <property name="fooService">
    <ref bean="fooService" />
    </property>
    </bean>
    </property>
    </bean>
-->

[/code]
Dao接口FooDAO.java
[code="java"]
package transaction;

public interface FooDAO{
String getFoo(String content);

void insertFoo(String foo);

void updateFoo(String foo,String newfoo);

}[/code]
Dao实现类FooDAOImpl.java
[code="java"]package transaction;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class FooDAOImpl extends JdbcDaoSupport implements FooDAO {

public String getFoo(String content) {
    JdbcTemplate jt=this.getJdbcTemplate();    
    String name = "";
    name = (String) jt.queryForObject(
            "SELECT content FROM test WHERE content = ?", new Object[] { content },
            java.lang.String.class);
    return name;
}

public void insertFoo(String foo){
    JdbcTemplate jt=this.getJdbcTemplate();    
    jt.update("INSERT INTO test VALUES('"+foo+"')" );
}

//test2表在库里不存在,故意让它出错。
public void updateFoo(String foo,String newfoo) {
JdbcTemplate jt=this.getJdbcTemplate();

jt.update("update test2 set content ='"+newfoo+"' where content='"+foo+"'" );
}
}
[/code]
业务类,也是事务管理的那个类ManagerImpl.java
[code="java"]public class ManagerImpl {
private static final Logger log = Logger.getLogger(ManagerImpl.class);
private FooDAO fooDAO;

public void setFooDAO(FooDAO fooDAO) {
    this.fooDAO = fooDAO;
}
public void Test(){
    String foo= "";
    foo= fooDAO.getFoo("foo");
    log.info(foo);
    fooDAO.insertFoo("rree001");
     foo= fooDAO.getFoo("rree001");
    log.info(foo);
    fooDAO.updateFoo("foo","test");
    foo= fooDAO.getFoo("test");
    log.info(foo);
}

}
[/code]
测试类Boot.java
[code="java"]
package transaction;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public final class Boot {

 public static void main(final String[] args) throws Exception {
     Resource resource = new FileSystemResource("src/applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        ManagerImpl managerImpl = (ManagerImpl) factory.getBean("managerImpl");
            managerImpl.Test();
      }

}
[/code]
测试结果是insert成功,update失败但是事务没起作用,没有回滚,表里插入了记录。改成上面的Spring配置文件中注释的部分就能够回滚,表里没有插入记录。

  • 写回答

1条回答 默认 最新

  • iteye_521 2008-11-15 20:14
    关注

    [quote]
    [color=red]BeanFactory factory = new XmlBeanFactory(resource); [/color]
    [/quote]
    把你的BeanFactory换成ApplicationContext,Spring文档中说的很清楚了,看下面:
    [quote]
    if you are using just a plain BeanFactory then a fair amount of support [color=red]such as transactions and
    AOP will not take effect/color, which could be confusing because
    nothing will actually be wrong with the configuration.
    [/quote]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题