I have an Spring+Hibernate/Flex application that needs to switch dynamically between database schemas. To accomplish that I implemented a AbstractRoutingDataSource following
It's clear that selection of a particular DataSource
actually happens when AbstractRoutingDataSource.getConnection()
is called, i.e. when transaction-bound Hibernate Session
is created. In your case it happens when you enter a @Transactional
method.
Thus, you can't switch schemes inside a transaction. You have to execute separate transactions against different schemes. To execute several transactions inside the same method you can use programmatic transaction management (TransactionTemplate
) instead of @Transactional
.