Self-invocation behaviour in @Configuration vs. @Component classes

Regular Spring @Components For an explanation of how normal Spring (AOP) proxies or dynamic proxies (JDK, CGLIB) in general work, see my other answer with illustrative sample code. Read that first and you will understand why self-invocation cannot be intercepted for these types of proxies via Spring AOP. @Configuration classes As for @Configuration classes, they … Read more

How to flush data into db inside active spring transaction?

Finally I stuck to the following solution: First, my @Test methods are not running within spring @Transactional support. See this article to know how dangerous it may be. Next, instead of using @Repository beans inside @Test methods I autowire @Service beans which use @Transactional annotation. The miracle is that @Test method like this @Test @Transactional(propagation … Read more

@Transactional method calling another method without @Transactional anotation?

When you call a method without @Transactional within a transaction block, the parent transaction will continue to the new method. It will use the same connection from the parent method (with @Transactional) and any exception caused in the called method (without @Transactional) will cause the transaction to rollback as configured in the transaction definition. If … Read more

Spring Transactions and hibernate.current_session_context_class

When using spring and spring managed transactions never mess around with the hibernate.current_session_context_class property UNLESS you are using JTA. Spring will by default set its own CurrentSessionContext implementation (the SpringSessionContext), however if you set it yourself this will not be the case. Basically breaking proper transaction integration. The only reason for changing this setting is … Read more

The matching wildcard is strict, but no declaration can be found for element ‘tx:annotation-driven’

You have some errors in your appcontext.xml: Use *-2.5.xsd xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd” Typos in tx:annotation-driven and context:component-scan (. instead of -) <tx:annotation-driven transaction-manager=”transactionManager” /> <context:component-scan base-package=”com.mmycompany” />

Spring @Transactional not working

The reason that moving the context:component-scan tags to the application context xml fixed the transactional behavior is: <tx:annotation-driven /> is a post-processor that wraps @Transactional annotated bean methods with an AOP method interceptor which handles transactional behavior. Spring post-processors, only operate on the specific application context they are defined in. In your case, you have … Read more

Spring – Is it possible to use multiple transaction managers in the same application?

Where you use a @Transactional annotation, you can specify the transaction manager to use by adding an attribute set to a bean name or qualifier. For example, if your application context defines multiple transaction managers with qualifiers: <bean id=”transactionManager1″ class=”org.springframework.orm.jpa.JpaTransactionManager”> <property name=”entityManagerFactory” ref=”entityManagerFactory1″ /> <qualifier value=”account”/> </bean> <bean id=”transactionManager2″ class=”org.springframework.orm.jpa.JpaTransactionManager”> <property name=”entityManagerFactory” ref=”entityManagerFactory2″ /> <qualifier … Read more