我用的是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);
}
}
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);
}
}
super.setDefaultRollback(false);
设置基类的这个属性
<aop:pointcut id="allManagerMethod" expression="execution(* com.test.service.impl.*.*(..))"/>
改到接口的那个包层就可以了。