Spring 3.1.3 + Hibernate Configuration with annotations and with (Dynamic) AbstractRoutingDataSource

不羁岁月 提交于 2019-12-06 12:24:06

I found that the problem was in my Dao Layer. In server startup It is not possible to access the current session, so I've done something like:

try {
    Session session = securitySessionFactory.getCurrentSession();
    Criteria crit = session.createCriteria(clazz);
    return (List<T>) crit.list();
} catch (Exception e) {
    Session session = securitySessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    transaction.begin();
    Criteria crit = session.createCriteria(clazz);
    List<T> list = (List<T>) crit.list();
    session.disconnect();
    session.close();
    return list;
}

With this I can fill the RoutingDataSources properly and make the number of datasources a little dynamic (with filling a new entry in the DB and a simple server restart).

Take into account that lazy mappings will be disabled, so it may be useful to evaluate what will be need to be set to FetchType.Eager (with FetchMode.Subselect if more than 1 bag is needed to be initialized with it)

I'm going to edit the question in order to leave this as an example for configuring routing "dynamic" datasources for Spring 3.1.3 with annotations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!