2015-12-09 22 views
0

嗨我有一個問題在春季設置休眠。我能夠使其工作,但它創建了數據庫上的很多會話。從我注意到它爲我的spring.xml上的每個bean創建會話。既然我有6種豆類宣佈我也對應用程序的數據庫6屆開始這裏是我的代碼春季休眠會話問題

<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" 
    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" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     > 

    <!-- Uncomment and add your base-package here: <context:component-scan base-package="org.springframework.samples.service"/> --> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
     <property name="url" value="jdbc:oracle:thin:" /> 
     <property name="username" value="Use" /> 
     <property name="password" value="Pass" /> 
    </bean> 


    <!-- Hibernate 4 SessionFactory Bean definition --> 
    <bean id="hibernate4AnnotatedSessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="com.model" /> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.current_session_context_class">thread</prop> 
       <!-- <prop key="hibernate.current_session_context_class">managed</prop> --> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="format_sql">true</prop> 



      </props> 
     </property> 
    </bean> 





<bean id="PDao" class="com.PDaoImpl"> 
     <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /> 
    </bean> 

    <bean id="PService" class="com.PServiceImpl"> 
     <property name="pDao" ref="PDao" /> 
    </bean> 

    <bean id="MNDao" class="com.MNDaoImpl"> 
     <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /> 
    </bean> 

    <bean id="MNService" class="com.MNServiceImpl"> 
     <property name="MNDao" ref="MNDao" /> 
    </bean> 

    <bean id="SWDao" class="com.SWDaoImpl"> 
     <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /> 
    </bean> 

    <bean id="SWService" class="com.SWServiceImpl"> 
     <property name="SWDao" ref="SWDao" /> 
    </bean> 
+0

你爲什麼要將你的DAO定義爲Spring bean? –

+0

嗨,我剛剛按照教程設置春季冬眠 – Onedaynerd

回答

1

您需要使用transactionManager的爲您管理會議。

添加以下代碼行到spring.xml

.... 
<tx:annotation-driven transaction-manager="transactionManager" /> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="yourSessionFactory" /> 
</bean> 
.... 

然後,你必須標註服務IMPL類@Transactional( 「transactionManager的」),使transactionManager的通過會議管理事務

@Transactional("transactionManager") 
public class PServiceImpl implements PServiceImpl{ 
    .... 

建議您可以通過註釋替換DI的XML配置,以便在spring.xml中簡化 ,刪除所有的bean聲明(xxservice和xxxdao)並將其替換爲:<context:component-scan base-package="put here the package where your services and daos are lacated" />

您的服務必須是這樣的:

@Service 
@Transactional("transactionManager") 
public class XXXServiceImpl implements XXXService{ 

    @Autowired 
    private XXXDAO xxxDAO; 
    ... 
} 

而且你的DAO必須像:

@Repository 
public class XXXDAOImpl implements XXXDAO { 

    @Autowired 
    private SessionFactory sessionFactory; 
    ... 
} 

一兩件事,加上發送模式在文件中的配置頭,你spring.xml應該看起來像這樣:

<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:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 
    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/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd"> 
+0

我收到此錯誤org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException嵌套異常是org.xml.sax.SAXParseException; lineNumber:53; columnNumber:67; cvc-complex-type.2.4.c:匹配的通配符是嚴格的,但是對元素'tx:annotation-driven'沒有聲明 – Onedaynerd

+0

你的文件配置頭中缺少tx模式,你的spring.xml應該看起來像這個:再看看我的回答,我更新了它 –