1

我的理解@Transactional应该只应用于需要在事务中发生的服务方法(例如设置器)。假设我有以下两个类(分别是 DAO 层和服务层)......

@Service("playerService")
public class PlayerServiceImpl implements PlayerService {
    @Autowired
    private PlayerDao playerDao;

    @Override
    public List<Player> getAll() {
        return playerDao.getAll();
    }


    @Override
    @Transactional
    public void addAllPlayers(final List<Player> players) {
        playerDao.addAllPlayers(players);
    }
}

@Repository("playerDao")
public class PlayerDaoImpl implements PlayerDao {

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    @Override
    public List<Player> getAll() {
        return (List<Player>) sessionFactory.getCurrentSession()
                .createQuery("FROM Player").list();
    }
    @Override
    public void addPlayer(final Player player) {
        sessionFactory.getCurrentSession().save(player);
    }
}

现在,如果我称它为addAllPlayers()正常工作,则完全没有问题。但是当我使用时getAll(), sessionFactory.getCurrentSession 会抛出 HibernateException,没有为当前线程找到会话。

如果我@Transactional为 getAll() 添加到服务层,现在可以“正常”工作。这个问题是我不应该仅仅为了调用一个getter而打开一个事务。

谁能想到我需要添加@Transactionalgetter方法以使sessionFactory拥有当前会话的任何原因?我的 servlet-context.xml 和 persistance-context.xml 如下所示(这些都在我的 web.xml 中的 contextConfigLocation 中引用)

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />

    <mvc:annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources location="/resources/css/" mapping="/css/**" />
    <mvc:resources location="/resources/js/" mapping="/js/**" />
    <mvc:resources location="/resources/images/" mapping="/images/**" />
    <mvc:resources location="/resources/img/" mapping="/img/**" />
    <mvc:resources location="/favicon.ico" mapping="/favicon.ico" />
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>



    <context:component-scan base-package="com.footieview.app" />
    <beans:bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <beans:property name="mediaTypes">
            <beans:map>
                <beans:entry key="html" value="text/html" />
                <beans:entry key="json" value="application/json" />
            </beans:map>
        </beans:property>
        <beans:property name="defaultViews">
            <beans:list>
                <beans:bean
                    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                    <beans:property name="prefixJson" value="true" />
                </beans:bean>
            </beans:list>
        </beans:property>
    </beans:bean>
    <beans:bean id="PlayerImportDaoImpl"
        class="com.footieview.app.importer.dao.PlayerImportDaoImpl" />
    <beans:bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>

</beans:beans>

持久性上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
        <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url"
            value="jdbc:mysql://localhost/db" />
        <beans:property name="username" value="username" />
        <beans:property name="password" value="password" />
    </beans:bean>

    <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.footieview.app.entity.Player</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="packagesToScan" value="com.footieview.app.entity.*" />
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
                </beans:prop>
                <beans:prop key="hibernate.show_sql">false</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">create</beans:prop>
                <beans:prop key="hibernate.cache.use_second_level_cache">
                    true
                </beans:prop>
                <beans:prop key="hibernate.cache.provider_class">
                    org.hibernate.cache.EhCacheProvider
                </beans:prop>
                <beans:prop key="hibernate.cache.use_query_cache">
                    true
                </beans:prop>
                <beans:prop key="hibernate.cache.region.factory_class">
                    org.hibernate.cache.ehcache.EhCacheRegionFactory
                </beans:prop>
                <beans:prop key="hibernate.cglib.use_reflection_optimizer">
                    true
                </beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>
</beans:beans>
4

1 回答 1

5

@Transactional 不仅打开一个数据库事务,而且在 Hibernate 的情况下,如果没有,它还会创建一个休眠会话。一种典型的方法是使用OpenSessionInViewFilter它为每个 http 请求创建一个 Hibernate 会话。

如果您不想使用此过滤器,则还需要使用 @Transactional 注释 getter。

于 2013-05-16T07:15:04.803 回答