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 6179993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:46:03+00:00 2026-05-24T00:46:03+00:00

i have a Problem understanding the @Transactional and @TransactionConfiguration(defaultRollback = true) Annotations. I have

  • 0

i have a Problem understanding the @Transactional and @TransactionConfiguration(defaultRollback = true) Annotations.

I have a test Code which inserts a userAccount into the Database, then another account with the same name should be inserted, which should result in a DataIntegrityViolationException because the AccountName is marked as Unique. This works fine if @Transactional and @TransactionConfiguration(defaultRollback = true) is not sepcified at the TestClass level. But if Rollback is enabled i don’t get the Exception because even in the same method the Data is not inserted into the databasse. If i set a breakpoint after inserting the first Account, the Database is still empty.

Here is my Code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class DaoTests {

    @Autowired
    private Repository repo;
    @Autowired
    private AccountService userService;

    @Before
    public void orgInstAccount() {
        Organization o = new Organization();
        o.setName("Organisation 1");
        repo.saveEntity(o);

        Institution i1 = new Institution();
        i1.setName("xyz");
        i1.setOwningOrganization(o);
        repo.saveEntity(i1);

    }

    @Test(expected = DataIntegrityViolationException.class)
    public void saveUserFail() {

        Account user = new Account();
        user.setAccountname("chz");
        user.setPassword(userService.calcMD5Hash("123"));
        user.setOwningInstitution(repo.getInstitutionByName("xyz"));
        repo.saveEntity(user);
        Assert.assertNotNull(repo.getAccountByName("chz"));


        Account userNew = new Account();
        userNew.setAccountname("chz");
        userNew.setPassword(userService.calcMD5Hash("123"));
        userNew.setOwningInstitution(repo.getInstitutionByName("xyz"));
        repo.saveEntity(userNew);
        //Here the Exception should be thrown but everything works fine.

    }

}

The Repository Implementation is:

@Repository
@Transactional
@SuppressWarnings("unchecked")
public class RepositoryHibernateImpl implements Repository {

    @Autowired
    private SessionFactory factory;

    @Override
    public void saveEntity(Entity hce) {
        factory.getCurrentSession().save(hce);
    }
}

Maybe the Problem is because the Repository and the TestClass are marked with @Transactional?

Thank you in Advance.

  • 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-05-24T00:46:04+00:00Added an answer on May 24, 2026 at 12:46 am

    Call flush() which will try to call sql immediatly instead of deferring it till transaction boundary

    factory.getCurrentSession().flush()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to have a problem understanding how to conditionally test a boolean value
I have an understanding problem of how the following code works: XMLInputFactory xif =
I'm having a problem understanding what I'm doing wrong here. I have the code
I have a problem understanding some code fragment from Matlab Signal processing Toolbox fir2()
I think I have a problem in understanding the proper way of using MVC.
There are many skills a programmer could have (understanding the problem, asking good questions,
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem understanding the meaning of join in the following case: Thread t1=new
i have small problem understanding why my smart pointer class is leaking on self
i'm starting to work with php basics and i have some problem understanding how

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.