3

I am using @Transactional with spring boot and jpa. But it is not working. Can somebody help out.

My inserts are in myDAO which is autowired in service class. Below code is method of service class which is implementing service interface

class MyService implements Service {

@Transactional(rollbackFor = RuntimeException.class)
    public ResponseVO createOrder(params) {
    myDAO.insertInTable1(param);
    myDAO.insertInTable2(param);//I kept wrong table name in this query such that it throws exception
         }

 }
Naveen Yalla
  • 141
  • 1
  • 11
  • nothing is wrong here, give as more info :) – MariuszS Jan 21 '19 at 15:28
  • Your rollback is only for `RuntimeException`, and wrong table don't create an `RuntimeException`. Read the `@Transactional` manual. – Zorglube Jan 21 '19 at 15:29
  • @Zorglube, I tried with creating new Runtime Exception with new. Even though no rollback. – Naveen Yalla Jan 21 '19 at 15:33
  • It seems you don't understand what you're doing, you don't need to forge an exception yourself, you need to choose for what kind of exception you rollback or not. – Zorglube Jan 21 '19 at 15:36
  • From where gets the createOrder method called? – C. Weber Jan 21 '19 at 16:10
  • createOrder is called from controller class – Naveen Yalla Jan 21 '19 at 16:23
  • Show the `MyDao` implementation, also which database are you using? If you are using MySQL make sure you are using a table store that supports transactions. – M. Deinum Jan 21 '19 at 18:49
  • I am using MYSql, with engine MyISAM. in DAO I am just inserting data into table1 and table 2 using JPA save and saveall methods.(JPARepository) – Naveen Yalla Jan 22 '19 at 05:41
  • If you want to rollback a transaction on an Exception then the method `must` throw an Exception... You're not throwing one here... add `public ResponseVO createOrder(params) throws Exception {...}` – Edward J Beckett Jun 27 '19 at 18:09

5 Answers5

6

Issue is with MySQL Database engine. My engine was MYIsam which doesn't supports Transactions. I changed my DB engine to InnoDB and its working. Thanks for the contributions. Below are the queries for the same.

SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_NAME = 'tabel_name' AND TABLE_SCHEMA='db_name';

ALTER TABLE table_name ENGINE = INNODB;

Naveen Yalla
  • 141
  • 1
  • 11
  • For changing to INNODB using the hibernate dialect refer to this https://stackoverflow.com/questions/1459265/hibernate-create-mysql-innodb-tables-instead-of-myisam – Rishi Agrawal Dec 27 '19 at 16:55
2

Try this:

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = RuntimeException.class)

I hope it helps.

Cheers!!

Roberto Rodriguez
  • 3,179
  • 32
  • 31
2

RuntimeException are already handled

replace with @Transactional(rollbackFor = Exception.class) to manage all checked exception and will work

ValerioMC
  • 2,926
  • 13
  • 24
0

Its hard to tell from the amount of code given. I do know that you shouldn't need the rollbackFor if it is RuntimeException. Those are always rolled back. I am guessing your table name error is a SQLException which implements Exception. You would have to have rollbackFor be rollbackFor=SQLException.class, and I would only keep that in while verifying that rollbacks happen. Only add the rollbackFor if you are throwing checked exceptions in methods called by the service.

B-Wieg
  • 181
  • 14
  • I tried that also. But no use. But generally Runtime exception will handle all types of exceptions. So it shoudl catch this exception also and rollback. – Naveen Yalla Jan 21 '19 at 15:35
  • SQLException is a subclass of Exception, not RuntimeException. RuntimeException is a subclass of Exception. Only subclasses of RuntimeException would get rolled back. – B-Wieg Jan 21 '19 at 15:44
-1

In Spring Boot, OpenSessionInView is enabled by default, so your rollback does not work because each time you call myDAO.insertInTableX(), a Commit is performed.

So to remove this behaviour and have your @Transactional works correctly, add this :

spring.jpa.open-in-view=false

To your application.properties

See this question for more

Xavier
  • 66
  • 1
  • 6