2

This might be a repeat but i couldn't find suitable post myself.

My question is, how does it really work (how the spring/hibernate support) to manage a single transaction with multiple DAO classes?

Does it really mean that same JDBC connection is used across multiple DAOs that are participating in a transaction? I would like to understand the fundamentals here.

Thanks in advance Harinath

Harinath
  • 51
  • 7

1 Answers1

2

Using a simple example:

@Controller
@Transactional
@RequestMapping("/")
public class HomeController {
    @Inject
    private UserRepository userRepository;
    @Inject
    private TagRepository tagRepository;

    ...

    @RequestMapping(value = "/user/{user_id}", method = RequestMethod.POST)
    public @ResponseBody void operationX(@PathVariable("user_id") long userId) {
        User user = userRepository.findById(userId);
        List<Tags> tags = tagRepository.findTagsByUser(user);
        ...
    }

    ...
}

In this example your controller has the overarching transaction, thus the entity manager will keep track of all operations in this operationX method and commit the transaction at the end of the method. Spring's @Transactional annotation creates a proxy for the annotated class which wraps it's methods in a transaction when called. It achieves this through the use of AOP.

Regarding the connection to the database - it is generally obtained from a connection pool and uses the connection for the duration of the transaction, whereafter it returns it to the connection pool. Similar question answered here: Does the Spring transaction manager bind a connection to a thread?

EDIT: Furthermore, for the duration of the transaction, the connection is bound to the thread. In subsequent database operations, the connection is obtained every time by getting the connection mapped to the thread in question. I believe the TransactionSynchronizationManager is responsible for doing this. The docs of which you can find here: TransactionSynchronizationManager

Community
  • 1
  • 1
Markus Coetzee
  • 3,384
  • 1
  • 29
  • 26
  • Thanks Markus for the response. So the every time a transaction is started (either implicitely when nothing is mentioned or explicitely at the service class), the entity manager or session (of hibernate) are bound with the same JDBC connection until the transaction is completed. Ok thing I want to understand how exactly this happens (binding of same jdbc connection with entity manager /session.) – Harinath Nov 26 '13 at 23:48
  • Look into transaction propagation as well. Multiple @Transactional services called in a controller request will share a transaction by default. – Blake Caldwell Nov 27 '13 at 03:15
  • @Harinath For the duration of the transaction, the connection is bound to the thread. In subsequent database operations, the connection is obtained every time by getting the connection mapped to the thread in question. I believe the TransactionSynchronizationManager is responsible for doing this. – Markus Coetzee Nov 27 '13 at 07:22
  • The docs for TransactionSynchronizationManager: http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/transaction/support/TransactionSynchronizationManager.html – Markus Coetzee Nov 27 '13 at 07:23
  • I will update my answer if this is what you were looking to find out. – Markus Coetzee Nov 27 '13 at 07:24
  • Of course you would never actually make a controller method transactional though, right? :) – Alex Barnes Dec 16 '13 at 19:57