![](/img/trans.png)
[英]Spring @transactional does not start a transaction while testing with JUnit4
[英]Spring - @Transactional does not start transaction
之前,我有带有getTransaction()和commitTransaction()方法的BaseDAO,用于事务处理。 但是,当我通过延迟加载添加@OneToMany
关系时,出现了与没有Session等相关的错误。因此,我决定在EmployerService方法上使用@Transactional
批注:
package services;
import daos.interfaces.InterfaceEmployerDAO;
import dtos.EmployerDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import services.interfaces.InterfaceEmployerService;
import transformers.interfaces.InterfaceEmployerTransformer;
import java.util.List;
public class EmployerService implements InterfaceEmployerService {
@Autowired
private InterfaceEmployerDAO employerDAO;
@Autowired
private InterfaceEmployerTransformer employerTransformer;
@Override
@Transactional
public List<EmployerDTO> getAllEmployers() {
return employerTransformer.listToDTO(employerDAO.getAllEmployers());
}
(methods irrevelant at this moment)
}
并且<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
已添加到applicationContext.xml文件中(之所以粘贴,是因为我是Spring Annotations中的新手,知道解决问题最重要的方法,对此我深表歉意):
<?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:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="user" value="postgres" />
<property name="password" value="postgres" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>models.Employee</value>
<value>models.Employer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
<prop key="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</prop>
<prop key="hibernate.connection.username">postgres</prop>
<prop key="hibernate.connection.password">postgres</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">target/luceneIndex</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"></property>
</bean>
<bean class="services.EmployeeService"></bean>
<bean class="services.EmployerService"></bean>
<bean class="daos.EmployeeDAO"></bean>
<bean class="daos.EmployerDAO"></bean>
<bean class="transformers.EmployeeTransformer"></bean>
<bean class="transformers.EmployerTransformer"></bean>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<context:component-scan base-package="controllers" />
<mvc:annotation-driven/>
</beans>
我以为HibernateTransactionManager
和tx:annotation-driven
使其工作了,但是我错了-我得到了: daos.EmployerDAO.getAllEmployers
方法中**createCriteria is not valid without active transaction**
。 我很确定我无法正确配置transactionManager
。 如果有人决定帮助我,我将非常高兴-预先感谢您。 我还介绍了mvc-dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
再次感谢任何帮助。
找到解决方案 ,请参阅下面的答案。
我想我在这里找到了一个解决方案 : “弹簧,休眠和声明式事务实现:没有活动事务” 。 我必须删除此行:
<prop key="hibernate.current_session_context_class">thread</prop>
因为它禁用了Spring事务管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.