Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:19:09+00:00 2026-06-12T08:19:09+00:00

I am trying to write a piece of code in which I can see

  • 0

I am trying to write a piece of code in which I can see an @Transaction method being rolled back upon a RuntimeException. That should be the expected default behaviour, but it is not what I am seeing. Any ideas why?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:mrpomario/springcore/jdbc/jdbc-testenv-config.xml")
@Transactional // Will rollback test transactions at the end
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class TransactionalTest
{
    @Autowired
    FeedManagerOne feedManagerOne;

    @Test
    public void test_RuntimeExceptions_Rollback_Behaviour(){
         Feed bogus = new Feed("B", "B", false);

         assertFalse(feedManagerOne.exists(bogus));

         try {
              feedManagerOne.createFeedAndThrowRuntimeException(bogus);
         }  catch (RuntimeException e) { }

         // WRONG! feedManagerOne.exists(bogus) SHOULD return false, but returns true.
         assertFalse(feedManagerOne.exists(bogus));
    }

}

My Service:

@Service
public class FeedManagerOne {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Transactional(readOnly = true)
    public boolean exists(Feed feed) {
        String query = "SELECT COUNT(*) FROM feed WHERE name = ? AND url = ? AND is_active = ?";
        int total = jdbcTemplate.queryForInt(query, feed.getName(), feed.getUrl(), feed.isActive());
        boolean found = (total == 1);

        return found;
    }

    @Transactional
    public boolean createFeedAndThrowRuntimeException(Feed feed) {
        String query = "INSERT INTO feed (name, url, is_active) values (?, ?, ?)";
        int rowsChanged = jdbcTemplate.update(query, feed.getName(), feed.getUrl(), feed.isActive());
        boolean created = (rowsChanged == 1);

        if (true)
        {
            throw new RuntimeException();
        }

        return created;
    }
}

This is how I define my TransactionManager:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
    <jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="mrpomario/springcore/jdbc/testdb/schema.sql"/>
    <jdbc:script location="classpath:mrpomario/springcore/jdbc/testdb/test-data.sql"/>
</jdbc:embedded-database>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:19:11+00:00Added an answer on June 12, 2026 at 8:19 am

    It’s expected behaviour.

    When you throw an exception (that should cause rollback) from @Transactional method, Spring marks transcation to be rolled back at its end. So, if you throw an exception from the top-most @Transactional method in the call stack, transaction will be rolled back immediately.

    But in your case your test method is @Transactional as well, therefore you have a single transaction that spans the whole test method. It means that despite the fact that transaction is marked for rollback after calling createFeedAndThrowRuntimeException(), it’s not rolled back until the end of the test method, therefore the second call of exists() can observe the changes.

    So, if you want to see the rollback you need to make your test method non-transactional.

    Also I don’t see <tx:annotation-driven/> in your configuration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write out a piece of code that can reduce the LENGTH
I'm trying to write a piece of code which use the user choice in
I'm trying to write a piece of code that takes in a two digit
Hi I'm trying to write a piece of code for a simple verification method
I'm trying to write a unit test for a piece of code that generates
Am trying to write a piece of code which will allows the user to
So I'm trying to write a code that determines where shapes are being placed
I'm trying to write a piece of code which receives a string, uses the
I was trying to write a piece of code for converting a list of
Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.