1

Tried to configure Spring for tests with hibernate and transactions. Getting bean from app context which is marked with @Transactional transaction isn't intercepted. What I could miss in configuration?

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    <property name="dataSource" ref="dataSource"></property>
</bean>

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

<import resource="spring-dao.xml"/>

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


<bean id="userService" class="com.test.service.UserServiceimpl">
    <property name="userDao" ref="userDao"/>
</bean>

public interface UserService {

public abstract User loadUserById(long userId);

@Transactional
public abstract void doSomething();

}

public class UserServiceimpl implements UserService {
@Override
public void doSomething() {
    User user = loadUserById(1);
    user.fillUpMoney(999);
    userDao.update(user);
    throw new RuntimeException("Shpould be rollback");
}
whatswrong
  • 2,203
  • 4
  • 25
  • 36
  • Not sure that this is the cause of the exception, but Spring recommends putting the @Transactional on the implementing method, not on the interface method. – JB Nizet Jan 24 '12 at 16:49
  • Related: http://stackoverflow.com/q/4745798/342852 (Classes don't inherit annotations from their interfaces) – Sean Patrick Floyd Jan 24 '12 at 17:31
  • As I remember it depended on Proxy implementation. Anyway annotating class method spring doesn't intercept transaction... – whatswrong Jan 24 '12 at 18:20

2 Answers2

3

Don't annotate the abstract method as transactional, annotate the concrete implementation.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

Do not user BeanFactory ;)

http://forum.springsource.org/showthread.php?122292-Sprinng-doesnt-intercept-transaction

whatswrong
  • 2,203
  • 4
  • 25
  • 36