@事务性不工作spring 4 hibernate 4
我想使用spring的transactionManager。 我的spring配置如下:@事务性不工作spring 4 hibernate 4,spring,hibernate,transactions,Spring,Hibernate,Transactions,我想使用spring的transactionManager。 我的spring配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springf
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
depends-on="propertyPlaceholderConfigurer"
p:driverClassName="org.postgresql.Driver"
p:url="${db.url}"
p:username="${db.username}"
p:password="${db.password}"
destroy-method="close" />
<context:spring-configured />
<context:annotation-config />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
depends-on="flywayAutomaticMigrationBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.example" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.default_schema">restProj</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/example/db/hbm/user/AuthUser.hbm.xml</value>
<value>com/example/db/hbm/user/User.hbm.xml</value>
<value>com/example/db/hbm/user/Role.hbm.xml</value>
<value>com/example/db/hbm/user/Feature.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.example.model" >
<context:include-filter type="regex" expression=".*\.dao\..*DAO"/>
<context:include-filter type="regex" expression=".*\.logic\..*Mgr"/>
</context:component-scan>
<context:component-scan base-package="com.example.commons" >
<context:include-filter type="regex" expression=".*\.dao\..*DAO"/>
<context:include-filter type="regex" expression=".*\.logic\..*Mgr"/>
</context:component-scan>
</beans>
当我运行UserMgr.checkTransaction()时,数据不会保存。
谁能向我解释一下是什么错了吗
\uuuuuuuuuuuuuuuuuuuu更新
这是我的特色DAO保存方法:
public java.lang.Long save(com.example.model.user.Feature feature)
throws org.hibernate.HibernateException {
return (java.lang.Long) super.save(feature);
}
这是超类保存方法:
protected Serializable save(final Object obj) {
return save(obj, getSession());
}
以及getSession()方法从spring sessionFactory获取会话
这是我的FeatureDao getInstance()方法:
在您的情况下,Spring将默认使用JDK代理。这将要求您在接口级别拥有
@Transactional
要在类级别启用使用@Transactional
,您可以将Spring配置为使用cglib代理,如下所示
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
更多细节我认为您的FeatureDAO不是一个SpringBean=>它不是由SpringAOP=>包装的,它独立地管理会话和事务,而不寻址Spring事务。请添加FeatureDAO源代码,以便回答此问题。FeatureDAO已加载到我更新的帖子中。:)如果你自己在搞会议(即,
sessionFactroy.openSession
您将不会得到一个spring管理的会话,而是一个非管理的会话。非spring管理的会话也不会参与spring管理的事务。此外,我强烈建议您使用依赖注入,而不是使用getInstance()
方法,不管它是否委托给一个实用程序类(在最坏的情况下,每次您对getApplicationContext
进行反编译时,它都会构造一个新的上下文)。我使用sessionFactory.openSession来获取会话。我如何获取它?
public static com.model.user.dao.FeatureDAO getInstance() {
return com.example.core.spring.ApplicationContextUtil.getApplicationContext().getBean(com.example.model.user.dao.FeatureDAO.class);
}
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />