2013-05-15 35 views
1

我的理解是@Transactional应该只应用于需要在事务中发生的服务方法(例如setters)。说我有以下两类(DAO层分别服务层)...Spring需要@Transactional在getter服务上吗?

@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而打开一个事务。

任何人都可以想到为什么我需要在getter方法中添加@Transactional来让sessionFactory拥有当前会话吗?我的servlet-context.xml中和毅力-context.xml中显示如下

的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> 

持久性-context.xml的

(这些都在我的contextConfigLocation web.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

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

如果你不想使用这个过滤器,你也需要用@Transactional来注释获取者。

+0

您可以使用readOnly属性来减少性能影响(如果有的话) - @Transactional(readonly = true)。 – gkamal

+0

我是否认为@Transactional确实创建了一个性能影响,因为我的理解是,如果事务已打开,它将在事务期间锁定表。 – david99world

+0

事务不锁定表。只有修改。什么是锁定取决于数据库 - 对于oracle锁是行作用域,对于hsqldb它默认情况下是表。 – mrembisz