3

i'm working with spring jdbcTemplate in some desktop aplications.

i'm trying to rollback some database operations, but i don't know how can i manage the transaction with this object(JdbcTemplate). I'm doing multiple inserts and updates through a methods sequence. When any operation fails i need rollback all previous operations.

any idea?


Updated... i tried to use @Transactional, but the rolling back doesn't happend.

Do i need some previous configuration on my JdbcTemplate?

My Example:

  @Transactional(rollingbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
  public void Testing(){
    jdbcTemplate.exec("Insert into table Acess_Level(IdLevel,Description) values(1,'Admin')");
    jdbcTemplate.exec("Insert into table Acess_Level(IdLevel,Description) values(STRING,'Admin')");
    }

The IdLevel is a NUMERIC parameter, so... in our second command will occur an exception. When i see the table in database, i can see the first insert... but, i think this operation should be roll back.

what is wrong?

Cleiton Ribeiro
  • 359
  • 2
  • 5
  • 16
  • Only slapping an annotation on there isn't going to work. An annotation is just metadata, without adding anything that knows how to handle that annotation it does nothing. You need to add `@EnableTransactionManagement` to your configuration class or `` to xml. Also be aware that only external method calls will be transactional if you are calling the `Testing` method from the same class it won't be transactional. – M. Deinum Jan 29 '16 at 08:04

2 Answers2

4

JdbcTemplate doesn't handle transaction by itself. You should use a TransactionTemplate, or @Transactional annotations : with this, you can then group operations within a transaction, and rollback all operations in case of errors.

@Transactional
public void someMethod() {
  jdbcTemplate.update(..)
}
Jérémie B
  • 10,611
  • 1
  • 26
  • 43
  • 2
    The most part of examples that i found, show us how rollback when an exception occurs. Does exist anyway to manually commit with JdbcTemplate? i think would be more simple. – Cleiton Ribeiro Jan 28 '16 at 20:27
2

In Spring private methods don't get proxied, so the annotation will not work. See this question: Does Spring @Transactional attribute work on a private method?.

Create a service and put @Transactional on the public methods that you want to be transactional. It would be more normal-looking Spring code to have simple DAO objects that each do one job and have several of them injected than it would to have a complicated DAO object that performed multiple SQL calls within its own transaction.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Sorry... i wrote the example by the wrong way. I'm using public methods in real, but without success yet. – Cleiton Ribeiro Jan 28 '16 at 21:32
  • @Cleiton: without more details all i can say is, pick an example from the spring samples and try to emulate it. It sounds like you're trying to take shortcuts to reduce the number of objects involved. but hard to say without more code. – Nathan Hughes Jan 28 '16 at 21:35