多数据源动态切换时,getConnection()的会不会出现并发问题,导致连接被其他请求篡改?不明白为什么?
@IocBean
public class DyDataSorce implements DataSource{
@Inject DataSource dataSource; // 注入主库的数据源
@IocBean(name="myDao") // 通过@IocBean工厂方法,声明一个用于操作子库的Dao实例
public Dao myDao() {
return new Dao(this);
}
public Connection getConnection() throws SQLException {
Record govsess = (Record)Session.getSession("govsess");
if(govsess!=null){
String sess_user_code =Session.getUserSess("govsess","user_code");
String user_code = Util.subArea(sess_user_code);
String url = getURL(user_code);
System.out.println("url:"+url);
if(Strings.isNotBlank(url)){
return DriverManager.getConnection(url);
}
}
System.out.println("默认的:");
return dataSource.getConnection();
}
private String getURL(String user_code) {
//从数据库或者缓存中读取
return MysqlInfo.mysql_infos.get(user_code);
}