简体   繁体   中英

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. Since I have 6 beans declared I also have 6 session on the database on application start Here is my code

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

Add the following lines of code to your 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")
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" />

your service must look like this :

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

    @Autowired
    private XXXDAO xxxDAO;
    ...
}

And your dao must look like :

@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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM