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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:47:18+00:00 2026-05-15T00:47:18+00:00

I found out really strange behavior on relatively simple use case, probably I can’t

  • 0

I found out really strange behavior on relatively simple use case, probably I can’t understand it because of not deep knowledges of spring @Transactional nature, but this is quite interesting.

I have simple User dao that extends spring JpaDaoSupport class and contains standard save method:

@Transactional
public User save(User user) {
    getJpaTemplate().persist(user);
    return user;
}

If was working fine until I’ve add new method to same class: User getSuperUser(), this method should return user with isAdmin == true, and if there is no super user in db, method should create one. Thats how it was looking like:

 public User createSuperUser() {
    User admin = null;

    try {
        admin = (User) getJpaTemplate().execute(new JpaCallback() {
            public Object doInJpa(EntityManager em) throws PersistenceException {
                return em.createQuery("select u from UserImpl u where u.admin = true").getSingleResult();
            }
        });
    } catch (EmptyResultDataAccessException ex) {
        User admin = new User('login', 'password');
        admin.setAdmin(true);
        save(admin); // THIS IS THE POINT WHERE STRANGE THING COMING OUT
    }

    return admin;
}

As you see code is strange forward and I was very confused when found out that no transaction was created and committed on invocation of save(admin) method and no new user wasn’t actually created despite @Transactional annotation.

In result we have situation: when save() method invokes from outside of UserDAO class – @Transactional annotation counted and user successfully created, but if save() invokes from inside of other method of the same dao class – @Transactional annotation ignored.

Here how I was change save() method to force it always create transaction.

public User save(User user) {
    getJpaTemplate().execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {
            em.getTransaction().begin();
            em.persist(user);
            em.getTransaction().commit();
            return null;
        }
    });
    return user;
}

As you see I manually invoke begin and commit. Any ideas?

  • 1 1 Answer
  • 1 View
  • 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-15T00:47:19+00:00Added an answer on May 15, 2026 at 12:47 am

    I think declarative transactions with annotation is implemented on base of proxies.

    If you access your DAO through the dynamic proxy it checks whether there is an annotation and wraps it with a transaction.

    If you call your class from inside the class there is no way to intercept this call.

    To avoid the problem you could mark the createSuperuser method with an annotation too.

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

Sidebar

Related Questions

I just found out that I can write a really simple web server using
I just found out about yield return , I seems really nice. I use
This is probably really simple, I know you can change css properties with javascript
Highcharts doesn't really do error reporting, as some of you have probably found out.
Concerning my previous question , I found out that maven can't really output jboss
Today I found out that code like that works. That sounds really strange to
I found out about GWT's CustomScrollPanel and how you can customize the scroll bar,
I found out jQuery does not always create the correct DOM from an HTML
I found out during the debug that my db is in: /data/data/package-name/databases/... how can
I found out an application I wrote does not work properly under Windows Vista/7

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.