读书人

请问关于spring事务管理的怪有关问题

发布时间: 2013-01-28 11:49:56 作者: rapoo

请教关于spring事务管理的怪问题,事务没有自动提交。大哥们请帮帮忙!
最近想要搭建一个spring和hibernate的系统,一切准备就绪,但是在测试的时候发现了个怪问题。

问题是这样的,代码可以正常运行,但是测试数据入库时发现不调用testDAO.getHibernateTemplate().getSessionFactory().close();这一句代码的话,在数据库里就查不到数据;调用这句之后就可以正常入库了。

实在是几天没搞定,想请各位大哥帮帮忙指点一下。

运行环境是:
JDK1.6
spring3.0(由myeclipse8.5自动导入的)
hibernate3.3(由myeclipse8.5自动导入的)
oracle 10g_r2

先上测试代码:

1、调用main类


package test;

import www.am8.manage.TestManageImpl;

public class SSHTest {

public static void main(String args[])
{
TestManageImpl tmi=new TestManageImpl();

tmi.shiwuSaveTest();
}
}


2、manage类

package www.am8.manage;

import java.util.Calendar;

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

import www.am8.iface.TestManage;
import www.am8.pojo.Test;
import www.am8.pojo.TestDAO;

public class TestManageImpl implements TestManage {

private TestDAO iTestDAO;

public TestDAO getiTestDAO() {
return iTestDAO;
}

public void setiTestDAO(TestDAO iTestDAO) {
this.iTestDAO = iTestDAO;
}

public void shiwuSaveTest() {
Resource res =new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(res);
//从BeanFactory获取UserDAO
TestDAO testDAO = (TestDAO) factory.getBean("TestDAO");

Test t = new Test();
Calendar c = Calendar.getInstance();

t.setTestId(21);
t.setShiJian(c.getTime());
t.setVar2("中文!!");
t.setChangZhengShu(23551);
t.setShiJian2("");

testDAO.save(t);
//testDAO.getHibernateTemplate().getSessionFactory().close();
}
}


3、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:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>

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

<bean id="TestDAO" class="www.am8.pojo.TestDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<bean id="TestManageImpl" class="www.am8.manage.TestManageImpl">
<property name="iTestDAO">
<ref bean="TestDAO" />
</property>
</bean>

<bean id="spring" class="www.am8.springUtil.BeansUtil" />

<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!--事务传播特性为required -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,
类中所有方法需要,还需要参考tx:advice的设置 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* www.am8.manage.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
</beans>


[解决办法]
<bean id="dataSource_log_mysql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc_log_mysql.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc_log_mysql.url}</value>
</property>
<property name="username">
<value>${jdbc_log_mysql.username}</value>


</property>
<property name="password">
<value>${jdbc_log_mysql.password}</value>
</property>
<property name="initialSize">
<value>${jdbc_log_mysql.initialSize}</value>
</property>
<property name="maxActive">
<value>${jdbc_log_mysql.maxActive}</value>
</property>
<property name="maxIdle">
<value>${jdbc_log_mysql.maxIdle}</value>
</property>
<property name="defaultAutoCommit">
<value>${jdbc_log_mysql.defaultAutoCommit}</value>
</property>
</bean>
[解决办法]
你没有采用Spring为Hibernate提供的getHibernateTemplate() 方法啊也,
你那么用只是用了Spring的注入,根本也没让Spring去托管Hibernate,所以你配置的事务切面怎么会有效果。
[解决办法]
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<!--配置事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置通知器 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.svse.service.*Service.*(..))"/>
</aop:config>


<bean id="usersDao" class="com.svse.dao.hibernate.UsersDaoHibernate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="usersService" class="com.svse.service.impl.UsersServiceImpl">
<property name="usersDao" ref="usersDao"></property>
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
[解决办法]
貌似现在很多人是用自动代理对象.至于hibernate事务提交好像是自动进行的,只需要调用sping生成的对象

读书人网 >J2EE开发

热点推荐