0
我使用spring ibatis連接Oracle多用戶空間。spring3 ibatis2.x multiple datasource
看起來像用戶A,用戶B,用戶系統。
用戶系統始終使用。其他用戶,而在任務(線程)調度研究
spring.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="System" />
<property name="password" value="system" />
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="A" />
<property name="password" value="a" />
</bean>
<bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="B" />
<property name="password" value="b" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource2" />
</bean>
<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource3" />
</bean>
在道:
public class BaseDao extends SqlMapClientDaoSupport {
Logger log = Logger.getLogger(getClass());
@Resource(name = "sqlMapClient")
private SqlMapClient sqlMapClient;
@PostConstruct
public void initSqlMapClient() {
super.setSqlMapClient(sqlMapClient);
}
}
public class UserDao extends BaseDao {
public void test()
{
this.getSqlMapClientTemplate().queryForObject("....");
}
}
任務:在taskJob我想調度研究其他datasorce。如何實現它?
public class TaskJob {
//@Autowired
//UserDao userDao;
public void test(){
//to get other sqlmapclient in UserDao
userDao.test();//for User A,User B,User ....
}
}
謝謝,但是這個解決方案太麻煩了。有更簡單的方法嗎?Oracle用戶很多。我需要循環這些用戶的線程 – zt9788
SqlMapClientDaoSupport被設計爲只支持一個SqlMapClient ..所以我真的懷疑你可以用現有的代碼做任何事情。 – ArunM
我可以自己創建DataSource不使用spring(aop),如:for(DataSource dt ...)(new UserDao().set(dt);) – zt9788