如何处理应用容器中出现的 javax.resource.ResourceException

How to handle javax.resource.ResourceException occurring in app container

目标:我在 JBOSS 服务器中创建了两个数据源。我需要实现一个逻辑,以便如果第一个是 down/disabled/incorrectly 设置,我可以连接到第二个数据源。

为了实现这个,我想在第一个数据源不起作用时抛出异常,以便我可以处理它并转到第二个数据源。

我的问题是我无法处理异常,因为这是在容器中发生的。

代码:

public class DB2GDataSource { 私人 JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource db2DataSource) {
    this.jdbcTemplate = new JdbcTemplate(db2DataSource);
}

public JdbcTemplate findJdbcTemplate() throws Exception {
// I was hoping this method to throw exception  but no luck
   return jdbcTemplate;
}

但是,我得到的异常是:

15:04:47,463 WARN  [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (JCA PoolFiller) IJ000610: Unable to fill pool: javax.resource.ResourceException: Could not create connection
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:288)
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:246)
    at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.createConnectionEventListener(SemaphoreArrayListManagedConnectionPool.java:788) [ironjacamar-core-impl-1.0.23.1.Final-redhat-1.jar:1.0.23.1.Final-redhat-1]
    at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.fillToMin(SemaphoreArrayListManagedConnectionPool.java:732) [ironjacamar-core-impl-1.0.23.1.Final-redhat-1.jar:1.0.23.1.Final-redhat-1]
    at org.jboss.jca.core.connectionmanager.pool.mcp.PoolFiller.run(PoolFiller.java:97) [ironjacamar-core-impl-1.0.23.1.Final-redhat-1.jar:1.0.23.1.Final-redhat-1]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_60]
Caused by: com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][4.14.113] Exception java.net.ConnectException: Error opening socket to server adc1db2d.ipc.us.aexp.com/148.171.100.10 on port 17,330 with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
    at com.ibm.db2.jcc.am.ed.a(ed.java:320)
    at com.ibm.db2.jcc.am.ed.a(ed.java:338)
    at com.ibm.db2.jcc.t4.vb.a(vb.java:434)
    at com.ibm.db2.jcc.t4.vb.<init>(vb.java:93)
    at com.ibm.db2.jcc.t4.a.b(a.java:354)
    at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2030)
    at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:731)
    at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:680)
    at com.ibm.db2.jcc.t4.b.<init>(b.java:334)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:232)
    at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:198)
    at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:475)
    at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:116)
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:260)
    ... 5 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method) [rt.jar:1.8.0_60]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) [rt.jar:1.8.0_60]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) [rt.jar:1.8.0_60]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) [rt.jar:1.8.0_60]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) [rt.jar:1.8.0_60]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) [rt.jar:1.8.0_60]
    at java.net.Socket.connect(Socket.java:589) [rt.jar:1.8.0_60]
    at com.ibm.db2.jcc.t4.v.run(v.java:49)
    at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.8.0_60]
    at com.ibm.db2.jcc.t4.vb.a(vb.java:420)
    ... 16 more

我发现异常是因为,当我通过 datasource.

设置 jdbcTemplate
public void setDataSource(DataSource db2DataSource) {
    this.jdbcTemplate = new JdbcTemplate(db2DataSource);
}

我处理了对 jdbcTemplate 构造的调用,首先检查 data-source 是否设置正确,现在我可以处理这种情况了。