教主无忌 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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧