Spring动态切换数据源出现的问题,请大家帮忙看看
项目中配置了2个数据源A和B,登录时从A取用户信息,然后从B取权限信息。
但是从A取出用户信息后,切换到数据源B时,有些时候会出现ORA-00942: table or view does not exist错误
应该是数据源还未切换完成就执行了hibernate操作的原因,请问这个问题如何解决
相关代码如下:
DataSourceContextHolder.setDataSourceType("A");
String sql = "select userId from user where userName="+name;
//hibernate查询操作略。。。。
DataSourceContextHolder.setDataSourceType("B");
String sql1 = "select t from UserRole t where t.userId='"+userId+"'";
Query query = getSession().createQuery(sql1);
//.......
以上代码执行到查询时,偶尔会报错,感觉是数据源还未切换就执行了下面的代码
013-12-23 14:12:17,225 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-10.4.0.137-8080-15) SQL Error: 942, SQLState: 42000
2013-12-23 14:12:17,225 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-10.4.0.137-8080-15) ORA-00942: table or view does not exist
2013-12-23 14:12:17,225 ERROR [STDERR] (http-10.4.0.137-8080-15)
org.hibernate.exception.SQLGrammarException: ORA-00942: table or view does not exist
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.exception.internal.Standard
SQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at $Proxy927.executeQuery(Unknown Source)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.loader.Loader.getResultSet(Loader.java:1962)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.loader.Loader.doQuery(Loader.java:829)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
2013-12-23 14:12:17,226 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.loader.Loader.doList(Loader.java:2447)
2013-12-23 14:12:17,227 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.loader.Loader.doList(Loader.java:2433)
2013-12-23 14:12:17,227 ERROR [STDERR] (http-10.4.0.137-8080-15) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2263)
201