Spring:如何手动重置连接池的连接?

时间:2018-08-28 07:44:30

标签: database spring connection-pooling

我正在尝试使用@Aspect手动验证数据库的运行状况。
而且,如果DatabaseA中发生了某些情况(在我的情况下是DatabaseA没有更多的写许可权),应用程序将切换到DatabaseB。
如何手动重新初始化连接池,以确保所有连接都将连接到DatabaseB?

@Aspect
@Component
@Order(value=1)
public class PostServiceAspect extends BaseService {

    @Before("execution(* com.some.class.execute(..))")
    public void databaseHealthCheck() {
        try {
            databaseDao.excuteHealthCheckSQL(); // execute "UPDATE table SET field = 1;"
        } catch (Exception ex) {
            databaseDao.switchTargetDatabase(); // switch to databaseB
            // How to re-initialize the connection pool
        }
    }

//DataSource config
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://testinghost:3306/devgpayment" />
    <property name="username" value="root" />
    <property name="password" value="password" />
    <property name="defaultAutoCommit" value="false" />
    <property name="maxTotal" value="40" />
    <property name="maxIdle" value="40" />
    <property name="maxWaitMillis" value="10000" />
    <property name="validationQuery" value="SELECT 1" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="5000" />
</bean>

0 个答案:

没有答案