Multi DataSources: 1 RW + many RO
-
30-03-2021 - |
Question
We have an app with single database 'dbMain', with set of read and write operations.
Now we need to add using extra databases db1-db10 for read-only operations, for using in SomeServiceImpl.
The problem is:
- I need to use several DAO (for dbMain and db1) at SomeServiceImpl
- only single transactionManager can be defined at tx:annotation-driven and @Transactional will operate with it only.
- sometimes I got db-connection leaks at SomeServiceImpl for operations with db1DAO. At least in @PostConstruct init() {...}
Really, don't want to use JTA. What is the proper way to configure such environment?
<tx:annotation-driven transaction-manager="dbMainTransactionManager"/>
<bean id="dbMainSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dbMainDataSource"/>
...
</bean>
<bean id="dbMainTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="dbMainSessionFactory"/>
...
</bean>
@Repository
public class DBMainDAOImpl extends HibernateDaoSupport implements DBMainDAO {
@Resource(name = "dbMainSessionFactory")
protected void init(SessionFactory sessionFactory) throws Exception {
setSessionFactory(sessionFactory);
}
...
}
@Transactional
@Service
public class SomeServiceImpl implements SomeService {
@Autowired
private DBMainDAO dbMainDAO;
...
}
Extra dbs:
<bean id="db1SessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="db1DataSource"/>
...
</bean>
<bean id="db1TransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="db1SessionFactory"/>
...
</bean>
public class DB1DAOImpl extends HibernateDaoSupport implements DB1DAO {
@Resource(name = "db1SessionFactory")
protected void init(SessionFactory sessionFactory) throws Exception {
setSessionFactory(sessionFactory);
}
...
}
@Transactional
public class SomeServiceImpl implements SomeService {
@Autowired
private DBMainDAO dbMainDAO;
@Autowired
private DB1DAO db1DAO;
...
@Autowired
private DB10DAO db10DAO;
@PostConstruct init() {
...
}
...
}
Pas de solution correcte
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow