日期:2014-05-17  浏览次数:20611 次

Spring+hibernate事务不起作用
分不多,希望大家帮忙看看
配置文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
<property name="jdbcUrl"
value="jdbc:sqlserver://localhost:1433;databaseName=TestData"></property>
<property name="user" value="sa"></property>
<property name="password" value="123"></property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingLocations">
<value>/WEB-INF/config/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
                </prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<aop:config>
<aop:pointcut expression="execution(* com.train.spring.service.impl.*.*(..))"
id="pointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>


<bean id="baseDao" class="com.train.spring.dao.BaseHibernateDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

BaseHibernateDao.java
public class BaseHibernateDao {

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public Session getSession(){
return sessionFactory.openSession();
}

}

serviceimp类操作
public User getUser() {
User user = dao.getUser();