Search code examples
javaspringjdbcrollbackspring-data-jdbc

Rollback in multiples querys using JDBC spring


i have this problem, when i execute two querys in same function, if the second query throw exception and not working, the first query keep yor result in database, both querys are insert in ddbb EXAMPLE:

public void myFunction(){
  query1(); //insert data in ddbb
  query2(); // also try to insert data in ddbb but not working and throw exception
}

in this case i need do rollback in both querys but currently the query1 doesn't rollback and keep his insert in datbase.

I am using spring data JDBC

thanks and sorry for my english, not is my native language


Solution

  • You could annotate your method with @Transactional, which would instruct Spring to rollback the entire method in the event of an exception:

    @Transactional
    public void myFunction(){
        // insert data in ddbb
        // also try to insert data in ddbb but not working and throw exception
    }