How to set autocommit to false in spring jdbc template

The problem is that you are setting autocommit on a Connection, but JdbcTemplate doesn’t remember that Connection; instead, it gets a new Connection for each operation, and that might or might not be the same Connection instance, depending on your DataSource implementation. Since defaultAutoCommit is not a property on DataSource, you have two options: Assuming … Read more

Why do spring/hibernate read-only database transactions run slower than read-write?

Why do spring/hibernate read-only database transactions run slower than read-write? <tldr> The short answer to question #1 was that hibernate starts off a @Transaction(readOnly = true) session with a set session.transaction.read.only synchronous JDBC call and ends with a set session.transaction.read.write call. These calls are not sent when doing read-write calls which is why read-only calls … Read more

Spring JPA repository transactionality

You are right. Only CRUD methods (CrudRepository methods) are by default marked as transactional. If you are using custom query methods you should explicitly mark it with @Transactional annotation. @Repository public interface UserRegistrationRepository extends JpaRepository<UserRegistration, Long> { UserRegistration findByEmail(String email); @Transactional void deleteByEmail(String email); } You should also be aware about consequences of marking repository … Read more

Spring nested transactions

This documentation covers your problem – https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/data-access.html#transaction-declarative-annotations In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if … 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” />

Synchronising transactions between database and Kafka producer

I’d suggest to use a slightly altered variant of approach 2. Write into your database only, but in addition to the actual table writes, also write “events” into a special table within that same database; these event records would contain the aggregations you need. In the easiest way, you’d simply insert another entity e.g. mapped … Read more

Spring transaction REQUIRED vs REQUIRES_NEW : Rollback Transaction

Using REQUIRES_NEW is only relevant when the method is invoked from a transactional context; when the method is invoked from a non-transactional context, it will behave exactly as REQUIRED – it will create a new transaction. That does not mean that there will only be one single transaction for all your clients – each client … 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

How to manually force a commit in a @Transactional method? [duplicate]

I had a similar use case during testing hibernate event listeners which are only called on commit. The solution was to wrap the code to be persistent into another method annotated with REQUIRES_NEW. (In another class) This way a new transaction is spawned and a flush/commit is issued once the method returns. Keep in mind … Read more

What is the difference between defining @Transactional on class vs method

In case 1 @Transactional is applied to every public individual method. Private and Protected methods are Ignored by Spring. Spring applies the class-level annotation to all public methods of this class that we did not annotate with @Transactional. However, if we put the annotation on a private or protected method, Spring will ignore it without … Read more