我用的是Spring+Hibernate,想测试一下Spring的事务管理。结果一测试了居然只生成SQL语句,数据库里却没有数据这之间没有报错。。无报错信息。。我估计是事务挂起了?但是又没提交,是配置问题还是我方法调用问题呢?我把我的代码贴出来哈。
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>    <session-factory>
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///hbtest</property>
        <property name="connection.username">root</property>
        <property name="connection.password">sa</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>

<mapping resource="com/test/entity/User.hbm.xml"/>
<mapping resource="com/test/entity/Log.hbm.xml"/>
    </session-factory></hibernate-configuration>
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: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.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
   </property>
</bean>
 
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
   </property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
   <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="del*" propagation="REQUIRED"/>
    <tx:method name="modify*" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
   </tx:attributes>
</tx:advice>
<!--那些类的哪些方法参与事务-->
<aop:config>
   <aop:pointcut id="allManagerMethod" expression="execution(* com.test.service.impl.*.*(..))"/>
   <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config> 

<bean id="userServiceImpl" class="com.test.serivce.impl.UserServiceImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
 
</beans>
Service层实现类:UserServiceImplpackage com.test.serivce.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.test.entity.Log;
import com.test.entity.User;
import com.test.serivce.UserService;/**
 * @author wangking E-mail:[email protected]
 * @version 创建时间:2009-9-7 上午01:34:52
 * 类说明
 */
public class UserServiceImpl extends HibernateDaoSupport implements UserService {

public void addUser(User user) {

this.getHibernateTemplate().save(user);
Log log = new Log();
log.setLogCont(new StringBuffer().append(user.getUsername()).append("is creating..").toString());
this.getHibernateTemplate().save(log); 
} public void deleteUser(User user) {
System.out.println("deleting the username");
}}
POJO我就不贴出来了,也就一个Log对象和一个User对象。
直接贴测试类:package com.test;import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.test.entity.User;
import com.test.serivce.UserService;
import com.test.serivce.impl.UserServiceImpl;/**
 * @author wangking E-mail:[email protected]
 * @version 创建时间:2009-9-7 上午01:49:20
 * 类说明
 */
public class Test {

public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceImpl service = (UserServiceImpl)factory.getBean("userServiceImpl");
User user = new User();
user.setUsername("wangking");
service.addUser(user);
}

}

解决方案 »

  1.   

    使用AbstractTransactionalDataSourceSpringContextTests 测试
                      
    super.setDefaultRollback(false);
    设置基类的这个属性
      

  2.   

    我测试了的,之所以生成SQL语句,但是数据库里没有数据,但又没有报错信息。。那是因为压根就没报错信息,事务操作完了后回滚了。。我就纳闷了,为什么他要回滚了?我又没配置其他的,方法的执行过程中又没有运行时异常。
      

  3.   

    是因为你的连接根本没有commit,所以断开以后当然回滚了.
      

  4.   

    设置了Spring的事务。还需要自己手动commit吗?好像事务已经帮我们处理了吧?
      

  5.   

    这个问题倒是解决了,是因为MYSQL默认innodb没有开启事务,是自动提交导致的,在MYSQL命令里set autocommit = 0;就行了,随之新问题又来了,我的程序测试回滚的时候,他没回滚!汗。
      

  6.   

    事务配置的那个切面要映射到业务接口层,不能到具体的实现层。
     <aop:pointcut id="allManagerMethod" expression="execution(* com.test.service.impl.*.*(..))"/>
    改到接口的那个包层就可以了。