如果我正确理解了这个问题,那么您就会遇到问题,因为即使DefaultDS数据源报告它已经启动,但由于它没有获得任何连接,您不一定知道可以建立连接。
不幸的是,即使启用了预填充选项,即使无法建立连接,数据源服务仍将正常启动。
最好的办法是实现一个ServiceMBean,它在报告启动之前检查来自数据源的实际连接。对于此示例,我们将其命名为org.bob.ConnChecker并将使用 ObjectName org.bob:service=ConnChecker进行部署。
您的部署描述符应如下所示:
<mbean code="org.bob.ConnChecker" name="jboss.mq:service=DestinationManager">
<depends optional-attribute-name="DataSource">jboss.jca:name=DefaultDS,service=ManagedConnectionPool</depends>
</mbean>
因此,在数据源启动之前,您的服务不会启动。除非它可以建立连接,否则您的服务将不会启动。现在您只需添加org.bob:service=ConnChecker作为 DestinationManager 的依赖项:
jboss.mq:service=MessageCache jboss.mq:service=PersistenceManager jboss.mq:service=StateManager jboss.mq:service=ThreadPool jboss:service=命名org.bob:service=ConnChecker
ConnChecker的代码如下所示:
....
import org.jboss.system.ServiceMBeanSupport;
....
public class ConnChecker extends ServiceMBeanSupport implements ConnCheckerMBean {
/** The ObjectName of the data source */
protected ObjectName dataSourceObjectName = null;
/** The Datasource reference */
protected DataSource dataSource = null;
/**
* Called by JBoss when the dataSource has started
* @throws Exception This will happen if the dataSource cannot provide a connection
* @see org.jboss.system.ServiceMBeanSupport#startService()
*/
public void startService() throws Exception {
Connection conn = null;
try {
// Get the JNDI name from the DataSource Pool MBean
String jndiName = (String)server.getAttribute(dataSourceObjectName, "PoolJndiName");
// Get a ref to the DataSource from JNDI
lookupDataSource(jndiName);
// Try getting a connection
conn = dataSource.getConnection();
// If we get here, we successfully got a connection and this service will report being Started
} finally {
if(conn!=null) try { conn.close(); } catch (Exception e) {}
}
}
/**
* Configures the service's DataSource ObjectName
* @param dataSourceObjectName The ObjectName of the connection pool
*/
public void setDataSource(ObjectName dataSourceObjectName) {
this.dataSourceObjectName = dataSourceObjectName;
}
/**
* Acquires a reference to the data source from JNDI
* @param jndiName The JNDI binding name of the data source
* @throws NamingException
*/
protected void lookupDataSource(String jndiName) throws NamingException {
dataSource = (DataSource)new InitialContext().lookup(jndiName);
}
}
ConnCheckerMBean的代码如下所示:
....
import org.jboss.system.ServiceMBeanSupport;
....
public interface ConnCheckerMBean extends ServiceMBean {
public void setDataSource(ObjectName dataSourceObjectName);
}
因此,如果无法与数据库建立连接,您仍然会遇到错误,但 DestinationManager 不会启动,希望这会比您现在遇到的头痛问题要好。