Question

My application is spring,struts2 and hibernateand database is postgress 9.2. Problem is DAO method not commiting data with out using flush.

My spring.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee     http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">




<context:component-scan base-package="dao" />

<jee:jndi-lookup id="depo" jndi-name="java:/depo"/>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="depo"/>


    <property name="hibernateProperties">
        <props>
            <!--    <prop     key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> -->
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.format_sql">true</prop>
            <!--    <prop key="hibernate.hbm2ddl.auto">update</prop> -->
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>

            <value>model.AtOrganisation</value>

        </list>
    </property>

</bean>
<bean id="transactionManager"   class="org.springframework.transaction.jta.JtaTransactionManager" />

<tx:annotation-driven transaction-manager="transactionManager" />


<bean id="orgdao" class="dao.OrganisationDaoImp">
    <property name="sessionfactory" ref="sessionFactory" />
</bean>

<bean id="empAction" class="action.OraganisationAction">
    <property name="orgdao" ref="orgdao" />
</bean>

And my DAO class is :

 @Override
@Transactional(propagation = Propagation.REQUIRED)
public void addOrg(AtOrganisation org) {

    Session session = sessionfactory.openSession();

    session.saveOrUpdate(org);
    //session.flush();


}

Can any one point out what did I miss here. Note: If I uncomment flush() data will commit.

Was it helpful?

Solution

You actually need to retrieve the same session that Spring is using by calling:

Session session = sessionFactory.getCurrentSession();

The call to openSession() will create an entirely new session that is not being managed by Spring, as opposed to the call to getCurrentSession(), that retrieves the same session managed by the @Transactional mechanism.

OTHER TIPS

First of all annotate method with the @Transactional annotation on the service level and leave DAO without Transactional annotation. Then let's see how it's working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top