I have a databaseChanges method that iterates 2 operations: A, B. First, "A", "B" is the last. "A" and "B" can be C reate, U pdate D Elemental functions in my persistent storage, Oracle Database 11g.
Let's say
'A' update the entry in the Users table, zip attribute, where id = 1.
'B' insert record into hobby tables.
Scenario: The DatabaseChanges method is called, "A" is running and updating the record. "B" is working and trying to insert a record, something happened, an exception is thrown, the exception boils to the databaseChanges method.
Expected: "A" and "B" did not change anything. the update that was made by "A" will be a rollback. "B" didn't change anything, well ... there was an exception.
Actual: Update "A" does not seem to roll back. "B" didn't change anything, well ... there was an exception.
Some code
If I had a connection, I would do something like:
private void databaseChanges(Connection conn) { try { conn.setAutoCommit(false); A(); //update. B(); //insert conn.commit(); } catch (Exception e) { try { conn.rollback(); } catch (Exception ei) { //logs... } } finally { conn.setAutoCommit(true); } }
Problem: I have no connection (see tags that are posted with the question)
I tried:
@Service public class SomeService implements ISomeService { @Autowired private NamedParameterJdbcTemplate jdbcTemplate; @Autowired private NamedParameterJdbcTemplate npjt; @Transactional private void databaseChanges() throws Exception { A();
My AppConfig class:
import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; @Configuration public class AppConfig { @Autowired private DataSource dataSource; @Bean public NamedParameterJdbcTemplate namedParameterJdbcTemplate() { return new NamedParameterJdbcTemplate(dataSource); } }
'A' makes an update. an exception is selected from "B". The update that was made by "A" is not a rollback.
From what I read, I understand that I am not using @Transactional correctly. I read and tried several blog posts and stackverflow Q and A without success to solve my problem.
Any suggestions?
EDIT
There is a method that calls databaseChanges ()
public void changes() throws Exception { someLogicBefore(); databaseChanges(); someLogicAfter(); }
Which method should be annotated with @Transactional,
changes ()? databaseChanges ()?