Home How does transaction propagation impact update in the database
Reply: 0

How does transaction propagation impact update in the database

user3257
1#
user3257 Published in June 21, 2018, 8:40 am

I am trying to understand the behavior of transaction propagation using SpringJTA - JPA - Hibernate.

Essentially I am trying to update an entity. To do so I have written a test method where I fetch an object using entity manager (em) find method ( so now this object is manged object). Update the attributes of the fetched object. And then optionally make a call to service layer(service layer propagation=required) which is calling em.merge

Now I have three variations here :

  1. Test method has no transactional annotation. Update the attributes of the fetched object and make no call to service layer.

    1.1. Result level 1 cache doesn't gets updated and no update to DB.

  2. Test method has no transactional annotation. Update the attributes of the fetched object. Call the service layer.

    2.1. Result level 1 cache and DB gets updated.

  3. Test method has Transnational annotation which could be any of the following. Please see the table below for Propagation value at the test method and the outcome of a service call.

(service layer propagation=required)

So to read the above table, the row 1 says if the Test method has transaction propagation= REQUIRED and if a service layer call is made then the result is update to Level 1 cache but not to the DB

Below is my test case

    @Test

    public void testUpdateCategory() {
//Get the object via entity manager
        Category rootAChild1 = categoryService.find(TestCaseConstants.CategoryConstant.rootAChild1PK);
        assertNotNull(rootAChild1);
        rootAChild1.setName(TestCaseConstants.CategoryConstant.rootAChild1 + "_updated");

// OPTIONALLY call update
    categoryService.update(rootAChild1);

//Get the object via entity manager. I believe this time object is fetched from L1 cache. As DB doesn't get updated but test case passes
Category rootAChild1Updated = categoryService.find(TestCaseConstants.CategoryConstant.rootAChild1PK);
        assertNotNull(rootAChild1Updated);
        assertEquals(TestCaseConstants.CategoryConstant.rootAChild1 + "_updated", rootAChild1Updated.getName());
        List<Category> categories = rootAChild1Updated.getCategories();
        assertNotNull(categories);
        assertEquals(TestCaseConstants.CategoryConstant.rootAChild1_Child1,categories.get(0).getName());
    }

Service Layer

 @Service
    public class CategoryServiceImpl implements CategoryService {   
        @Transactional
        @Override
        public void update(Category category) {
            categoryDao.update(category);
        }
    }

DAO

 @Repository
    public class CategoryDaoImpl {
    @Override
        public void update(Category category) {
            em.merge(category);
        }
    }

Question Can someone please explain why does REQUIRED, REQUIRES_NEW, and NESTED doesn't lead to insertion in the DB?

And why absence of transaction annotation on Test case lead to insertion in the DB as presented in my three variations?

Thanks

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314165 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO