简体   繁体   English

春季休眠会议问题

[英]Spring Hibernate session issue

Hi I'm having a problem setting up hibernate on spring. 嗨,我在春季设置休眠状态时遇到问题。 I was able to make it work but it creates a lot of session on the database. 我能够使其工作,但是它在数据库上创建了很多会话。 From What i have notice it creates session for every bean on my spring.xml. 从我已经注意到的内容中,它为spring.xml上的每个bean创建会话。 Since I have 6 beans declared I also have 6 session on the database on application start Here is my code 因为我声明了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>

You need to use transactionManager to manage session for you. 您需要使用transactionManager来管理会话。

Add the following lines of code to your spring.xml 将以下代码行添加到spring.xml中

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

Then you have to annotate your service impl classes @Transactional("transactionManager") to make transactionManager managing transactions through session 然后,您必须注释服务隐含类@Transactional(“ transactionManager”),以使transactionManager通过会话管理事务

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

Just an advice you can replace XML config for the DI by annotations to make it easy in spring.xml, remove all your beans declarations (xxservice and xxxdao) and replace them by: <context:component-scan base-package="put here the package where your services and daos are lacated" /> 只是一个建议,您可以通过注解替换DI的XML配置,以使其在spring.xml中变得容易,删除所有bean声明(xxservice和xxxdao),并替换为: <context:component-scan base-package="put here the package where your services and daos are lacated" />

your service must look like this : 您的服务必须如下所示:

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

    @Autowired
    private XXXDAO xxxDAO;
    ...
}

And your dao must look like : 而且您的dao必须看起来像:

@Repository
public class XXXDAOImpl implements XXXDAO {

    @Autowired
    private SessionFactory sessionFactory;
    ...
}

One more thing, add the tx schema in your file config header, your spring.xml should look like this : 还有一件事,在文件配置头中添加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