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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:17:40+00:00 2026-06-06T11:17:40+00:00

I understand we need to keep @Transactional boundaries as short as possible. Here is

  • 0

I understand we need to keep @Transactional boundaries as short as possible. Here is the code :

I am using userDAO object through Spring dependency injection :

private static ApplicationContext context ;
private UserDAO userDAO;

public TransactionsTest() {
    userDAO = (UserDAO) context.getBean("userDAO");
} 

I am calling testSaveUserAccounts() from TransactionsTest class trying to use userDAO for insertion/updation of data.

Case 1:

@Transactional
public void testSaveUserAccounts() {
    UserAccounts userAccounts = new UserAccounts();
    userAccounts.setCommunityId(10L);
    userDAO.saveObject(userAccounts);
}

// This method is inside UserDAO
        public void saveObject(Object object) {
        entityManager.merge(object);
    }

Case 2:

@Transactional
public void testSaveUserAccounts() {
    UserAccounts userAccounts = new UserAccounts(); 
    userAccounts.setCommunityId(10L);
    userDAO.saveObject(userAccounts);
}

// This method is inside UserDAO
    @Transactional(propagation=Propagation.REQUIRED)
    public void saveObject(Object object) {
        entityManager.merge(object);
    }

Spring Context :

    <tx:annotation-driven transaction-manager="transactionManager" />

        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>

        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSourceLocal" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>
            <property name="persistenceUnitName" value="spring-jpa" />
      </bean>

UserDAO :

@Repository
public class UserDAO extends BaseDAO {

    @Transactional(propagation=Propagation.REQUIRED)
    public void saveObject(Object object) {
        entityManager.merge(object);
    }

}

BaseDAO :

public abstract class BaseDAO {

    protected EntityManager entityManager;
    protected HashMap<String,Long>  eventIdMap = new HashMap<String,Long>();


    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this. entityManager = entityManager;
    }


    public <T> T getById(final Class<T> clazz, final Serializable id) {
        T object = clazz.cast(entityManager.find(clazz, id));
        return object;
    }


    @Transactional
    public Object save(Object ob) {
        Object object = entityManager.merge(ob);
        return object;
    }


    @Transactional
    public void persist(Object ob) {
        entityManager.persist(ob);
    }


    @SuppressWarnings("unchecked")
    public <T>  ArrayList<T> getAll(final Class<T> clazz) {
        String hqlQuery = "from "+ clazz.getSimpleName();
        ArrayList<T> list  = (ArrayList<T>)entityManager.createQuery(hqlQuery).getResultList();
        return list;
    }

}

I have been experimenting around several transactional boundaries REQUIRED, REQUIRES_NEW, SUPPORTS, etc but couldn’t confidently make out as to why Case 1 (when method2 is called which is inside transactional boundary of method1) does not merges the data, while, this is solved in Case 2.

Why do I need to specify @Transactional in inner methods as well when already I have marked calling function within Transaction boundary ?

  • 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-06T11:17:42+00:00Added an answer on June 6, 2026 at 11:17 am

    Your transaction test class is not a Spring Bean that is why Case 1 does not work. Spring needs to detected that a method has @Transactional on it and it does that when spring registers the bean with the spring bean factory.

    Also keep in mind that the if you are doing Proxy Based AOP calls within the same bean will not be caught by the transaction aspect unless you use AspectJ load time weaving or AspectJ compile time weaving.

    Also putting @Transactional on your Dao’s is not a really a good idea because transaction boundaries are best marked at the service layer. The reason why is that a particular service method might need to interact with multiple Dao’s and you would want those Dao’s actions to be part of the tx started by the service layer, rather than having to analyze the Dao’s to see what the propagation behavior is.

    Can you post the complete code for the test class?

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

Sidebar

Related Questions

I need to understand how this is possible, i.e. I would like to understand
Im in need of some advice here to understand the cleanest way to split
I don't understand the need for self-joins. Can someone please explain them to me?
I need to understand how to create a simple animation in HTML 5 or
I need to understand how to use the generic Delphi 2009 TObjectList . My
I need to understand the working of this particular program, It seems to be
I understand that we need to create MXML file to define a view. Suppose
I understand that I need to figure out my own homework, but seeing that
I am new to php and need to understand how post get method could
I know this question is difficult to understand but I need a mathematical equation

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.