2

I want to use transactionManager of spring. My spring configuration is here:

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


<bean id="dataSource" 
        class="org.apache.commons.dbcp.BasicDataSource" 
        depends-on="propertyPlaceholderConfigurer"
        p:driverClassName="org.postgresql.Driver"
        p:url="${db.url}" 
        p:username="${db.username}" 
        p:password="${db.password}"
        destroy-method="close" />




<context:spring-configured />
<context:annotation-config />

<bean id="sessionFactory" 
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
        depends-on="flywayAutomaticMigrationBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.example" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.default_schema">restProj</prop>
        </props>
    </property>

    <property name="mappingResources">
        <list>
            <value>com/example/db/hbm/user/AuthUser.hbm.xml</value>
            <value>com/example/db/hbm/user/User.hbm.xml</value>
            <value>com/example/db/hbm/user/Role.hbm.xml</value>
            <value>com/example/db/hbm/user/Feature.hbm.xml</value>

        </list>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean> 

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

<context:component-scan base-package="com.example.model" >
    <context:include-filter type="regex" expression=".*\.dao\..*DAO"/>
    <context:include-filter type="regex" expression=".*\.logic\..*Mgr"/>
</context:component-scan>
<context:component-scan base-package="com.example.commons" >
    <context:include-filter type="regex" expression=".*\.dao\..*DAO"/>
    <context:include-filter type="regex" expression=".*\.logic\..*Mgr"/>
</context:component-scan>


</beans>

And this is one of my Manager class:

import org.springframework.transaction.annotation.Transactional;

public class UserMgr extends BaseUserMgr {

    @Transactional (rollbackFor = Exception.class)
    public void checkTransaction(){
        Feature feature = new Feature();
        feature.setExtuid(UUID.randomUUID().toString());
        feature.setName("boogh");
        feature.setDescription("salam");
        FeatureDAO.getInstance().save(feature);
    }   
}

When I run UserMgr.checkTransaction() Data doesn't save. Can anyone explane to me what is wrong?

__UPDATE___

This is my FeatureDao Save Method :

      public java.lang.Long save(com.example.model.user.Feature feature)
        throws org.hibernate.HibernateException {
        return (java.lang.Long) super.save(feature);
    }

and this is superclass Save method:

protected Serializable save(final Object obj) {
    return save(obj, getSession());
}

and the getSession() method get session from spring sessionFactory.

And this is my FeatureDao getInstance() method:

public static com.model.user.dao.FeatureDAO getInstance() {
    return com.example.core.spring.ApplicationContextUtil.getApplicationContext().getBean(com.example.model.user.dao.FeatureDAO.class);
}
littleali
  • 408
  • 1
  • 6
  • 22
  • 1
    I think your FeatureDAO is not a Spring bean => it's not wrapped by Spring AOP => it manages sessions and transactions independently without addressing to Spring transactions. **Please add FeatureDAO source code, for this question to be answered**. – Boris Treukhov Sep 02 '14 at 12:38
  • FeatureDao is load in – littleali Sep 02 '14 at 12:43
  • 1
    If you are messing around with the session yourself (i.e. `sessionFactroy.openSession` you will NOT get a spring managed session but an unmanaged one. A non spring managed session will also NOT participate in spring managed transactions. Also I strongly urge you to use dependency injection instead of hacking around with a `getInstance()` method, regardless of the fact that that is delegating to a utility class (which in the worst case would construct a new context each time you de a `getApplicationContext`). – M. Deinum Sep 02 '14 at 13:01
  • I get my session useing `sessionFactory.openSession` how can I get it ? – littleali Sep 02 '14 at 13:10
  • You should use getCurrentSession() instead – Boris Treukhov Sep 02 '14 at 13:16
  • When I changed `sessionFactory.openSession` to `sessionFacotry.getCurrentSession()` this exception was thrown: `Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)` – littleali Sep 02 '14 at 13:21

1 Answers1

0

In your case Spring is going to use JDK proxies by default. That would require you to have @Transactional at the interface level.

To enable using @Transactional at class level you can configure Spring to use cglib proxies as follows

<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />

More details here

Community
  • 1
  • 1
Varun Phadnis
  • 671
  • 4
  • 6