spring配置多数据源,中间用了aop来完成动态的切换,另外在所有DAO层方法上添加了before的切面
原因:当在service层调用dao层进行数据库处理时,若service 没有启动事务机制,则执行的顺序为:切面——>determineCurrentLookupKey——>Dao方法。而当在service层启动事务时,由于在一个事务中执行失败后会回滚之前所执行的所有操作,因此spring会在service方法执行前调用determineCurrentLookupKey,此时无论service中有多少个dao调用determineCurrentLookupKey将不再执行,即在事务中不支持数据源切换。
如:
@Transactional(propagation = Propagation.REQUIRED) //顺序为:切面——>determineCurrentLookupKey——>Dao方法
public String getJsonList(PageBean page,Map<String,String> map,String className,String order){
List<T> list=new ArrayList<T>();
if(order.equals("null"))
list=getList(page,map,className);
else
list=getList(page,map,className,order);
int rowCount = dao.getCount("select count(*) from "+className+Utils.getHql(map));
HashMap<String, Object> tempMap = new HashMap<String, Object>();
tempMap.put("Rows", list);
tempMap.put("Total", rowCount);
JSONObject json = new JSONObject();
json.putAll(tempMap);
return json.toString();
}
@Transactional(propagation = Propagation.SUPPORTS)//顺序为:determineCurrentLookupKey——>切面——>Dao方法
public String getJsonList(PageBean page,Map<String,String> map,String className,String order){
List<T> list=new ArrayList<T>();
if(order.equals("null"))
list=getList(page,map,className);
else
list=getList(page,map,className,order);
int rowCount = dao.getCount("select count(*) from "+className+Utils.getHql(map));
HashMap<String, Object> tempMap = new HashMap<String, Object>();
tempMap.put("Rows", list);
tempMap.put("Total", rowCount);
JSONObject json = new JSONObject();
json.putAll(tempMap);
return json.toString();
}
待验证-------