Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Declarative Transaction not rolling back
    primarykey
    data
    text
    <p>I created a simple spring application to test the basics of Spring declarative transaction. As per rules declarative transaction should rollback in case of RuntimeException. But it was not rolling back in my case. </p> <p>Main test class has code</p> <pre><code>public class SpringOraTest { public static void main(String[] args) { ApplicationContext aplctx= new FileSystemXmlApplicationContext("src\\config\\SpringConfigForOra.xml"); //Call to test Declarative Transaction with Annotation TrxHandleAnnotated prxyobj=((TrxHandleAnnotated)aplctx.getBean("dbCommandAnnotated")); prxyobj.doTask(); } } </code></pre> <p>The class TrxHandleAnnotated had code :-</p> <pre><code>@Transactional public class TrxHandleAnnotated public void doTask(){ ApplicationContext aplctx= new FileSystemXmlApplicationContext("src\\config\\SpringConfigForOra.xml"); JdbcTemplate jdbcTemplate= (JdbcTemplate)aplctx.getBean("jdbcTemplate"); jdbcTemplate.update("insert into kau_emp values(4,'forthmulga' )"); throw new RuntimeException(); } </code></pre> <p>And there was required configuration in config XML.</p> <p>I was expecting that transaction be rolled back when exception is thrown. But it was not rolled back and record was getting commited to DB.</p> <p>Even after long search on internet I could not understand why it was not getting rolled back.</p> <p>Later I realised that, in doTask() code I am creating context once again and taking our JdbcTemplate instance out of new context it. This was the root cause of the issue. </p> <p>I changed the code such a way that both classes will use some context. And it worked !!!</p> <pre><code>public class SpringOraTest { public static ApplicationContext aplctx; public static void main(String[] args) { aplctx= new FileSystemXmlApplicationContext("src\\config\\SpringConfigForOra.xml"); //Call to test Declarative Transaction with Annotation TrxHandleAnnotated prxyobj= ((TrxHandleAnnotated)aplctx.getBean("dbCommandAnnotated")); prxyobj.doTask(); } @Transactional public class TrxHandleAnnotated public void doTask(){ JdbcTemplate jdbcTemplate=(JdbcTemplate)SpringOraTest.aplctx.getBean("jdbcTemplate"); jdbcTemplate.update("insert into kau_emp values(4,'forthmulga' )"); throw new RuntimeException(); } </code></pre> <p>This is a lesson-learnt for me that, unless otherwise required, whole application should use only one context object.</p> <p>This will sound too obvious Spring practitioners but spring novice like me can do such silly mistakes. So thought of sharing it.</p> <p>In this particular case instead of manually creating JdbcTemplate is better to declare it as member variable and use setter injection. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload