簡體   English   中英

春季休眠會議問題

[英]Spring Hibernate session issue

嗨,我在春季設置休眠狀態時遇到問題。 我能夠使其工作,但是它在數據庫上創建了很多會話。 從我已經注意到的內容中,它為spring.xml上的每個bean創建會話。 因為我聲明了6個bean,所以我在應用程序啟動時也在數據庫上進行了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>

您需要使用transactionManager來管理會話。

將以下代碼行添加到spring.xml中

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

然后,您必須注釋服務隱含類@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;
    ...
}

還有一件事,在文件配置頭中添加tx模式,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">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM