使用InitialContext在类中使用wildfly数据源

时间:2017-09-14 11:54:19

标签: java datasource wildfly

我正在使用带有两个数据源的Wildfly 10.1版本,如下所示

    <subsystem xmlns="urn:jboss:domain:datasources:4.0">
        <datasources>
            <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:mysql://${dbhostname}:3306/${dbname}</connection-url>
                <driver>h2</driver>
                <security>
                    <user-name>sa</user-name>
                    <password>sa</password>
                </security>
            </datasource>
            <datasource jta="true" jndi-name="java:jboss/datasources/jbpmDS" pool-name="MySQLDS" enabled="true" use-java-context="true" use-ccm="true">
                <connection-url>jdbc:mysql://${dbhostname}:3306/${dbname}</connection-url>
                <driver>mariadb</driver>
                <new-connection-sql>select 1</new-connection-sql>
                <pool>
                    <min-pool-size>2</min-pool-size>
                    <max-pool-size>10</max-pool-size>
                    <prefill>true</prefill>
                    <flush-strategy>FailingConnectionOnly</flush-strategy>
                </pool>
               <security>
                    <user-name>${dbuser}</user-name>
                    <password>${dbpassword}</password>
                </security> 
                <validation>
                    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
                    <check-valid-connection-sql>select 2</check-valid-connection-sql>
                    <validate-on-match>true</validate-on-match>
                    <background-validation>true</background-validation>
                    <background-validation-millis>780000</background-validation-millis>
                    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
                </validation>
                <timeout>
                    <idle-timeout-minutes>9</idle-timeout-minutes>
                </timeout>
                <statement>
                    <track-statements>nowarn</track-statements>
                    <share-prepared-statements>false</share-prepared-statements>
                </statement>
            </datasource>

在我的java代码中(它将驻留在我在wildfly服务器上运行的应用程序的一个jar中)我已经制作了如下代码来获取数据源

Context initialContext = new InitialContext();
DataSource datasource = (DataSource)initialContext.lookup("java:jboss/datasources/jbpmDS");
connection = datasource.getConnection();
prepStatement = connection.prepareStatement(sqlQuery);
resultSet = prepStatement.executeQuery();

正好在datasource.getConnection()行,我得到以下异常,

java.sql.SQLException: javax.resource.ResourceException: IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@183cd1f[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@1bd4a01 connection handles=0 lastReturned=1505387264452 lastValidated=1505387157714 lastCheckedOut=1505387264436 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@19c5e2b mcp=SemaphoreConcurrentLinkedQueueManagedConnectionPool@a50680[pool=MySQLDS1] xaResource=LocalXAResourceImpl@41a9e5[connectionListener=183cd1f connectionManager=74916c warned=false currentXid=null productName=MySQL productVersion=5.7.19-log jndiName=java:jboss/datasources/jbpmDS] txSync=null]
    at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:146)
    at org.jboss.as.connector.subsystems.datasources.WildFlyDataSource.getConnection(WildFlyDataSource.java:66)
    at com.abcd.efgh.handler.util.AbcdBPMDBUtil.getSbEncryptDecryptKeyFromDB(AbcdBPMDBUtil.java:33)
    at com.abcd.efgh.handler.util.AbcdBPMHandlerConstants.<clinit>(AbcdBPMHandlerConstants.java:718)
    at com.abcd.efgh.handler.util.AbcdBPMLogger.<clinit>(AbcdBPMLogger.java:23)
    at com.abcd.efgh.handler.generic.AbcdBPMServiceInvocationHandler.<clinit>(AbcdBPMServiceInvocationHandler.java:38)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.mvel2.util.ParseTools.createClass(ParseTools.java:503)
    at org.mvel2.util.ParseTools.findClass(ParseTools.java:926)
    at org.mvel2.ast.NewObjectNode.getReducedValue(NewObjectNode.java:260)
    at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:101)
    at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:47)
    at org.mvel2.MVEL.eval(MVEL.java:150)
    at org.mvel2.ast.InlineCollectionNode.execGraph(InlineCollectionNode.java:178)
    at org.mvel2.ast.InlineCollectionNode.execGraph(InlineCollectionNode.java:137)
    at org.mvel2.ast.InlineCollectionNode.getReducedValue(InlineCollectionNode.java:104)
    at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:101)
    at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:47)
    at org.mvel2.MVEL.eval(MVEL.java:165)
    at org.drools.core.util.MVELSafeHelper$RawMVELEvaluator.eval(MVELSafeHelper.java:441)
    at org.drools.core.SessionConfigurationImpl.loadWorkItemHandlers(SessionConfigurationImpl.java:318)
    at org.drools.core.SessionConfigurationImpl.initWorkItemHandlers(SessionConfigurationImpl.java:308)
    at org.drools.core.SessionConfigurationImpl.getWorkItemHandlers(SessionConfigurationImpl.java:284)
    at org.drools.core.impl.StatefulKnowledgeSessionImpl.getWorkItemManager(StatefulKnowledgeSessionImpl.java:1908)
    at org.drools.core.command.runtime.process.RegisterWorkItemHandlerCommand.execute(RegisterWorkItemHandlerCommand.java:68)
    at org.drools.core.command.runtime.process.RegisterWorkItemHandlerCommand.execute(RegisterWorkItemHandlerCommand.java:32)
    at org.drools.core.fluent.impl.PseudoClockRunner.executeBatch(PseudoClockRunner.java:102)
    at org.drools.core.fluent.impl.PseudoClockRunner.executeBatches(PseudoClockRunner.java:69)
    at org.drools.core.fluent.impl.PseudoClockRunner.execute(PseudoClockRunner.java:61)
    at org.drools.core.fluent.impl.PseudoClockRunner.execute(PseudoClockRunner.java:39)
    at org.drools.core.command.impl.AbstractInterceptor.executeNext(AbstractInterceptor.java:39)
    at org.drools.persistence.PersistableRunner$TransactionInterceptor.execute(PersistableRunner.java:594)
    at org.drools.persistence.PersistableRunner$TransactionInterceptor.execute(PersistableRunner.java:552)
    at org.drools.core.command.impl.AbstractInterceptor.executeNext(AbstractInterceptor.java:39)
    at org.drools.persistence.jpa.OptimisticLockRetryInterceptor.internalExecute(OptimisticLockRetryInterceptor.java:102)
    at org.drools.persistence.jpa.OptimisticLockRetryInterceptor.execute(OptimisticLockRetryInterceptor.java:83)
    at org.drools.persistence.jpa.OptimisticLockRetryInterceptor.execute(OptimisticLockRetryInterceptor.java:44)
    at org.drools.core.command.impl.AbstractInterceptor.executeNext(AbstractInterceptor.java:39)
    at org.drools.persistence.jta.TransactionLockInterceptor.execute(TransactionLockInterceptor.java:73)
    at org.drools.persistence.jta.TransactionLockInterceptor.execute(TransactionLockInterceptor.java:45)
    at org.drools.persistence.PersistableRunner.execute(PersistableRunner.java:398)
    at org.drools.persistence.PersistableRunner.execute(PersistableRunner.java:66)
    at org.drools.core.runtime.InternalLocalRunner.execute(InternalLocalRunner.java:37)
    at org.drools.core.runtime.InternalLocalRunner.execute(InternalLocalRunner.java:41)
    at org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession$1.registerWorkItemHandler(CommandBasedStatefulKnowledgeSession.java:181)
    at org.jbpm.runtime.manager.impl.AbstractRuntimeManager.registerItems(AbstractRuntimeManager.java:132)
    at org.jbpm.runtime.manager.impl.SingletonRuntimeManager.init(SingletonRuntimeManager.java:116)
    at org.jbpm.runtime.manager.impl.RuntimeManagerFactoryImpl.newSingletonRuntimeManager(RuntimeManagerFactoryImpl.java:64)
    at org.jbpm.kie.services.impl.AbstractDeploymentService.commonDeploy(AbstractDeploymentService.java:135)
    at org.jbpm.kie.services.impl.KModuleDeploymentService.deploy(KModuleDeploymentService.java:197)
    at org.kie.server.services.jbpm.JbpmKieServerExtension.createContainer(JbpmKieServerExtension.java:388)
    at org.kie.server.services.impl.KieServerImpl.createContainer(KieServerImpl.java:280)
    at org.kie.server.services.impl.ContainerManager.installContainersSync(ContainerManager.java:43)
    at org.kie.server.services.impl.ContainerManager.installContainers(ContainerManager.java:33)
    at org.kie.server.jms.ContainerManagerEJB.installContainers(ContainerManagerEJB.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:82)
    at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:93)
    at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:63)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.invocationmetrics.ExecutionTimeInterceptor.processInvocation(ExecutionTimeInterceptor.java:43)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.tx.EjbBMTInterceptor.handleInvocation(EjbBMTInterceptor.java:103)
    at org.jboss.as.ejb3.tx.BMTInterceptor.processInvocation(BMTInterceptor.java:58)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:73)
    at org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor.processInvocation(EjbRequestScopeActivationInterceptor.java:83)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ee.concurrent.ConcurrentContextInterceptor.processInvocation(ConcurrentContextInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:52)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.singleton.SingletonComponentInstanceAssociationInterceptor.processInvocation(SingletonComponentInstanceAssociationInterceptor.java:53)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:47)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:100)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.deployment.processors.StartupAwaitInterceptor.processInvocation(StartupAwaitInterceptor.java:22)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437)
    at org.jboss.as.ejb3.concurrency.ContainerManagedConcurrencyInterceptor.processInvocation(ContainerManagedConcurrencyInterceptor.java:110)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:67)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:54)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:636)
    at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:356)
    at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:198)
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:185)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.interceptors.LogDiagnosticContextRecoveryInterceptor.processInvocation(LogDiagnosticContextRecoveryInterceptor.java:82)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340)
    at org.jboss.as.ejb3.component.interceptors.AsyncFutureInterceptorFactory$1$2.runInvocation(AsyncFutureInterceptorFactory.java:104)
    at org.jboss.as.ejb3.component.interceptors.AsyncInvocationTask.run(AsyncInvocationTask.java:74)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)

如果有人能帮助我,我将非常感激。这个问题让我的工作停在中间。请尽快帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

根据this post,禁用jta,如此

datasource jta="false"

会吝啬这个错误。正如你所做的那样,根据你的评论。当然,只有当你真的不需要jta(并因此将事务类型默认为RESOURCE_LOCAL)时才能接受。如果你想(并且你应该)启用jta,我会继续搜索更长时间,this post应该告诉你如何。