当前位置:编程学习 > JAVA >>

spring的事务配置问题

不报错,但是没有储存到数据库中,求指导。


<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<bean id="BOffice" class="myjava.data.Office"></bean>
<bean id="BPeople" class="myjava.data.People"></bean>

<bean id="BOfficeDAO" class="myjava.data.OfficeDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="BPeopleDAO" class="myjava.data.PeopleDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<!--  配置事务管理器  -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!--  配置事务传播特性 -->
<tx:advice id="TestAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="*" propagation="REQUIRED"/>
      <tx:method name="save*" propagation="REQUIRED"/>
      <tx:method name="del*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="add*" propagation="REQUIRED"/>
      <tx:method name="find*" propagation="REQUIRED"/>
      <tx:method name="get*" propagation="REQUIRED"/>
      <tx:method name="apply*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
<!--  配置参与事务的类 -->
<aop:config>
<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*(..))"/>
<aop:advisor pointcut-ref="allTestServiceMethod" advice-ref="TestAdvice" />
</aop:config>

</beans>




package myjava.action;

import com.opensymphony.xwork2.ActionSupport;
import myjava.data.*;

public class SaveOfficeAction extends ActionSupport {

private IOffice BOffice;
private IOfficeDAO BOfficeDAO;
private String Tips;

public IOffice getBOffice() {
return BOffice;
}

public void setBOffice(IOffice bOffice) {
BOffice = bOffice;
}

public IOfficeDAO getBOfficeDAO() {
return BOfficeDAO;
}

public void setBOfficeDAO(IOfficeDAO bOfficeDAO) {
BOfficeDAO = bOfficeDAO;
}

public String getTips() {
return Tips;
}

public void setTips(String tips) {
Tips = tips;
}



public String execute() throws Exception{

this.BOfficeDAO.save(this.BOffice);
this.Tips=this.BOfficeDAO.findById(2).getOfficename().toString();
return SUCCESS;


}


}






但是换成如下的事务配置就可以储存。



<bean id="TOfficeDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref local="BOfficeDAO"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
--------------------编程问答-------------------- --------------------编程问答-------------------- 能运行就行了,哪种好用用哪种,好久没有过xml形式配置事务了 --------------------编程问答-------------------- 你上面那种配置的事务是声明试事务,目测 没发现什么问题啊,,,
这和下面的配置是两种不同的事务。。

--------------------编程问答-------------------- 后台有报错吗?看上去没什么错误 --------------------编程问答-------------------- 学习了,现在项目不怎么用xml配置了 --------------------编程问答-------------------- 其实我还是喜欢xml和annotation混搭的开发方式! --------------------编程问答-------------------- 咋看下没错。楼主晒晒
this.BOfficeDAO.save(this.BOffice);        this.Tips=this.BOfficeDAO.findById(2).getOfficename().toString();

两个注入方法的实现。 --------------------编程问答-------------------- <aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*(..))"/>
改为
<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*.*(..))"/>
试试 --------------------编程问答--------------------
引用 8 楼 oh_Maxy 的回复:
<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*(..))"/>
改为
<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*.*(..))"/>
试试


不行啊,运行出错啊! --------------------编程问答--------------------
引用 9 楼 sunrealonetwo 的回复:
Quote: 引用 8 楼 oh_Maxy 的回复:

<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*(..))"/>
改为
<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*.*(..))"/>
试试


不行啊,运行出错啊!

这么久了,问题还没解决啊 --------------------编程问答--------------------
引用 2 楼 a82759082 的回复:
能运行就行了,哪种好用用哪种,好久没有过xml形式配置事务了


不行啊,下面哪种每一个都需要配置一次,太麻烦了。无论如何查不出错误!也不报错。 --------------------编程问答--------------------
引用 10 楼 oh_Maxy 的回复:
Quote: 引用 9 楼 sunrealonetwo 的回复:

Quote: 引用 8 楼 oh_Maxy 的回复:

<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*(..))"/>
改为
<aop:pointcut id="allTestServiceMethod" expression="execution(* myjava.*.*.*(..))"/>
试试


不行啊,运行出错啊!

这么久了,问题还没解决啊


是啊,不报错,不存储,烦啊! --------------------编程问答-------------------- 要不,把这句去掉,看看能不能存,要能存,说明还是这句表达式有问题的。就一个个试试吧。。 --------------------编程问答--------------------
this.BOfficeDAO.save(this.BOffice);   
改为
this.BOfficeDAO.save(BOffice)试试;   
会不会this.BOffice还没拿到setter方法注入的值。然后原来的this.BOffice是空的。。。。 --------------------编程问答-------------------- BOfficeDAO.save(BOffice)
记得通过spring注入了。没必要强调this了吧
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,