2013-05-15 34 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