Dynamically changing session factory and Txm Manager for I18N support

≡放荡痞女 提交于 2019-12-24 10:36:58

问题


I have a peculiar issue here in the Spring-GWT application we are building. We have an oracle DB encoded in WE8ISO8859P1 character set which doesn't support UTF-8 . Hence we are building a new DB in AL32UTF8 char set . Unfortunately the DBAs do not want to migrate the existing DB to the new DB and we have to reach the old DB for the English data and the new DB for the Latin data .

We have @Transactional annotations at method/class level and the sessionFactory is injected into the DAO to connect to hibernate. I want to reuse these when user selects latin something like

@Transactional(changeThisDynamically)

i.e, the TransactionManager and SessionFactory injected should change dynamically via an Ajax call when the user switches between Latin/English.

Can this be done? What is is the best approach to resolve this ?

On second thought, I could get the Latin session factory by making the bean ApplicationContextAware and set this in the dao but but is this a good approach ? and what do I do with the TransactionManager ?

Thanks,


回答1:


Thanks for all your help. What I really needed was an 'AbstractRoutingDataSource' - I fixed this by doing the following

    <bean id="dataSource" class="com.myPackage.CustomRoutingDataSource">
   <property name="targetDataSources">
      <map key-type="com.myPackage.DBLocaleEnum">
         <entry key="English" value-ref="defaultDataSource"/>
         <entry key="Spanish" value-ref="latinDataSource"/>
      </map>
   </property>
   <property name="defaultTargetDataSource" ref="defaultDataSource"/>
</bean>

public class CustomRoutingDataSource extends AbstractRoutingDataSource {

@Override
protected Object determineCurrentLookupKey() {

    DBLocaleEnum localeType = LocaleContextHolder.getLocaleType();
    return localeType;
}

The return type of determineCurrentLookupKey method helps me determine which datasource I should be using .



来源:https://stackoverflow.com/questions/8268329/dynamically-changing-session-factory-and-txm-manager-for-i18n-support

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