JBoss AS 7关闭 - 在WAR部署停止之前关闭连接管理器 [英] JBoss AS 7 shutdown - connection manager closed before WAR deployment stopped

查看:287
本文介绍了JBoss AS 7关闭 - 在WAR部署停止之前关闭连接管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:JBoss AS 7.1.1.Final。

Env: JBoss AS 7.1.1.Final.

我有一个使用JBoss AS JNDI数据源的WAR应用程序。当我关闭服务器(控制台中的Ctrl + C)时,应用程序会收到一个shutdown命令并开始销毁它的Spring上下文。但是,我使用调度程序来执行某些数据库操作。当应用程序关闭时,我希望完成当前队列中的任务(但不接受新任务 - 标准JDK Executor.shutdown()行为)。当我取消部署应用程序而不停止服务器时,这很好。但是,当我停止整个服务器时,连接管理器在应用程序取消部署之前关闭,这导致

I have a WAR application using a data source taken from JBoss AS JNDI. When I shut down the server (Ctrl+C in the console), the application receives a shutdown command and starts to destroy its Spring context. However, I use a scheduler to perform some DB operations. When the application is closing, I want the tasks that are currently in the queue to be finished (but no new tasks are accepted - standard JDK Executor.shutdown() behaviour). This works fine when I undeploy the application without stopping the server. However, when I stop the whole server, the connection manager is closed before the application undeployment, which results in

14:31:51,604 INFO  [org.jboss.as.logging] JBAS011503: Restored bootstrap log handlers
14:31:51,617 INFO  [org.apache.coyote.http11.Http11Protocol] Stopping Coyote HTTP/1.1 on http-127.0.0.1-127.0.0.1-18080
14:31:51,638 INFO  [org.hornetq.ra.HornetQResourceAdapter] HornetQ resource adapter stopped
14:31:51,653 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/MY-APP]] Closing Spring root WebApplicationContext
14:31:51,656 INFO  [org.springframework.web.context.support.XmlWebApplicationContext] Closing Root WebApplicationContext: startup date [Tue Jul 09 14:30:56 CST 2013]; root of context hierarchy
14:31:51,659 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2c591927: defining beans [<snipped>]; root of factory hierarchy
14:31:51,662 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] HornetQ Server version 2.2.13.Final (HQ_2_2_13_FINAL_AS7, 122) [5f713ff6-5f86-11e2-a25d-1f3857764d50] stopped
14:31:51,673 INFO  [MY-APP.Shutdown] Initializing shutdown. Already running tasks will be finished, new tasks will not be executed.
14:31:53,626 ERROR [org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler] Unexpected error occurred in scheduled task.: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: javax.resource.ResourceException: IJ000451: The connection manager is shutdown: java:/my/DS1
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) [spring-jdbc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:575) [spring-jdbc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:818) [spring-jdbc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:874) [spring-jdbc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:882) [spring-jdbc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at com.my.app.scanner.db.SyncEntryRepository.deleteById(SyncEntryRepository.java:26) [classes:]
    at com.my.app.rules.orphanentries.OrphanedEntriesProcessor.process(OrphanedEntriesProcessor.java:22) [classes:]
    at com.my.app.routing.Router$RoutingWorker.performRouting(Router.java:49) [classes:]
    at com.my.app.routing.Router$RoutingWorker.route(Router.java:32) [classes:]
    at com.my.app.routing.Router.route(Router.java:18) [classes:]
    at com.my.app.transformation.Transformation.perform(Transformation.java:21) [classes:]
    at com.my.app.MyApp.run(MyApp.java:18) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_45]
    at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_45]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64) [spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53) [spring-context-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) [rt.jar:1.6.0_45]
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) [rt.jar:1.6.0_45]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) [rt.jar:1.6.0_45]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) [rt.jar:1.6.0_45]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:180) [rt.jar:1.6.0_45]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:204) [rt.jar:1.6.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [rt.jar:1.6.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [rt.jar:1.6.0_45]
    at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_45]
Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000451: The connection manager is shutdown: java:/my/DS1
    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:137)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) [spring-jdbc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) [spring-jdbc-3.2.3.RELEASE.jar:3.2.3.RELEASE]
    ... 26 more
Caused by: javax.resource.ResourceException: IJ000451: The connection manager is shutdown: java:/my/DS1
    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:321)
    at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)
    at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)
    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:129)
    ... 28 more

14:31:53,640 INFO  [MY-APP.Shutdown] Shutdown complete
14:31:53,651 INFO  [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] Closing JPA EntityManagerFactory for persistence unit 'default'
14:31:53,656 INFO  [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] Closing JPA EntityManagerFactory for persistence unit 'default'
14:31:53,837 INFO  [org.jboss.as.server.deployment] JBAS015877: Stopped deployment MY-APP.war in 2259ms
14:31:56,158 INFO  [com.arjuna.ats.jbossatx] ARJUNA032018: Destroying TransactionManagerService
14:31:56,158 INFO  [com.arjuna.ats.jbossatx] ARJUNA032014: Stopping transaction recovery manager
14:31:56,160 INFO  [org.jboss.as] JBAS015950: JBoss AS 7.1.1.Final "Brontes" stopped in 4567ms

在我的standalone.xml中我有

In my standalone.xml I have

<subsystem xmlns="urn:jboss:domain:datasources:1.0">
    <datasources>
        <datasource jndi-name="java:/my/DS1" pool-name="My1" enabled="true" use-java-context="true">
            <connection-url>jdbc:oracle:thin:@10.172.1.1:1521:ABCD</connection-url>
            <driver>oracle</driver>
            <pool>
                <min-pool-size>1</min-pool-size>
                <max-pool-size>10</max-pool-size>
                <prefill>true</prefill>
                <use-strict-min>false</use-strict-min>
            </pool>
            <security>
                <user-name>xxx</user-name>
                <password>xxx</password>
            </security>
            <timeout>
                <idle-timeout-minutes>5</idle-timeout-minutes>
            </timeout>
            <statement>
                <prepared-statement-cache-size>500</prepared-statement-cache-size>
                <share-prepared-statements>true</share-prepared-statements>
            </statement>
        </datasource>
            <drivers>
            <driver name="oracle" module="com.oracle"/>
        </drivers>
    </datasources>
</subsystem>

然后,我在代码中查找数据源:

then, I lookup the datasources in the code:

DataSource ds = new InitialContext().lookup("java:/my/DS1");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

数据源必须以编程方式查找,而不是在spring.xml中进行硬编码,因为可以存在多个数据源及其JNDI名称在应用程序在启动时扫描的外部属性文件中配置。

The datasource has to be looked up programmatically and not hard-coded in spring.xml since there can be multiple data sources and their JNDI names are configured in external properties file that the application scans on startup.

然后,将jdbcTemplate传递给使用Spring调度调用的TableScanner :

Then, the jdbcTemplate is passed to a TableScanner that is invoked with Spring scheduling:

public TableScanner(JdbTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

@Scheduled(fixedDelay = 5000)
public void run() {
    // query a table using jdbcTemplate and process the retrieved records, deleting them at the end of processing
}

在run()方法调用期间停止服务器时,应处理和删除所有检索到的记录。当应用程序尝试删除它们时,连接管理器已经关闭,因此异常。

When stopping of the server occurs during run() method invocation, all the retrieved records should be processed and deleted. When the application tries to delete them, the connection manager is already closed, hence the exception.

有没有办法建立连接管理器(或任何其他JBoss AS子系统)我可能需要等到应用程序停止?

Is there any way to make the connection manager (or any other JBoss AS subsystem I might need) to wait until the application is stopped?

推荐答案

我遇到了同样的问题,我现在找到了原因在 https://issues.jboss.org/browse/WFLY-944 中进行了描述。为避免这种情况而在那里提出的解决方案是将jndi声明为Web应用程序中的资源,以便在应用程序终止之前不与jboss服务器解除绑定。

I had the same problem, and I have now found the cause described in https://issues.jboss.org/browse/WFLY-944 . The solution proposed there in order to avoid it, is to declare the jndi as a resource in your web application, so that it does not unbind from the jboss server before your application terminates.

为实现此目的,请在web.xml中添加以下部分,该部分设置对名为jdbc / myDS的jboss jndi的引用。

To achieve this add the following part to your web.xml which sets a reference to your jboss jndi with name "jdbc/myDS".

<resource-ref>
<res-ref-name>jdbc/myDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<lookup-name>java:/my/DS1</lookup-name>
</resource-ref>

然后从代码部分查找java:/ my / DS1而不是指向实际的jndi,您现在应该查找java:comp / env / jdbc / myDS,它将引用您放在web.xml中的资源,该资源实际指向实际的jndi(我们之前指定为'lookup-name '在web.xml中添加的部分中的xml属性):

Then instead of looking up "java:/my/DS1" from your code part that points to the actual jndi, you should now instead look up "java:comp/env/jdbc/myDS" which will reference the resource you put in the web.xml that actually points to the actual jndi (that we previously specified as the 'lookup-name' xml property in the part added in the web.xml):


DataSource ds =(DataSource)
ctx.lookup( java:comp / env / jdbc / myDS);

DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDS");

这篇关于JBoss AS 7关闭 - 在WAR部署停止之前关闭连接管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆