JNDI数据库连接从WSO2数据源失败,但在context.xml中工作

时间:2016-02-04 22:04:29

标签: wso2 wso2as

我正在编写一个小型的Web服务,它将数据保存在数据库中。我正在使用WSO2 Application Server 5.2.1和Oracle Database XE 11g以及在Windows 7上进行开发/测试。

当我在[AppServerHome] /repository/conf/tomcat/context.xml中创建资源时,代码可以正常工作。当我在管理控制台下创建数据源并将其作为JNDI数据源公开时,getConnection调用失败,并带有SQLException。

如果我使用管理控制台中创建的数据源通过管理控制台创建数据服务,则数据服务也可以使用。这应该表明数据源有效且有效。

我尝试了jdbc url的不同变体:

jdbc:oracle:thin:@localhost:1521:xe
jdbc:oracle:thin:@localhost:1521/xe
jdbc:oracle:thin:@localhost:1521:XE
jdbc:oracle:thin:@localhost:1521/XE

我还尝试检查“使用数据源工厂”选项,而不是真正理解它的作用。它也没有明显的效果。

我尝试按照https://docs.wso2.com/display/AS520/Exposing+Datasources+as+JNDI+Resources的说明操作,但也没有效果。

任何人都可以帮助解释为什么数据源不适用于webapp但context.xml资源是什么?我需要使数据源工作,以便我在context.xml文件中没有纯文本密码,因此严格通过WSO2管理控制台管理数据源和密码。

有效的context.xml中的资源是:

<Resource name="jdbc/db1" auth="Container"
     type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
     maxActive="100" maxIdle="30" maxWait="10000"
     url="jdbc:oracle:thin:@localhost:1521:xe"
     username="user" password="aPasswordGoesHere"/>

我在Management Console下创建的数据源包含以下信息:

Data Source Type: RDBMS
Name: MyDB
Data Source Provider: default
Driver: oracle.jdbc.OracleDriver
URL: jdbc:oracle:thin:@localhost:1521:xe
Username: user
Password: aPasswordGoesHere

Expose as a JNDI Data Source:
Name: jdbc/db1
Use Data Source Factory: unchecked
JNDI Properties: none set

Data Source Configuration Parameters:
(No changes made)

我的META-INF / context.xml中匹配的ResourceLink

<ResourceLink name="jdbc/db1" global="jdbc/db1" type="javax.sql.DataSource" />

用于获取DataSource的代码是:

        Connection conn;

        try
        {
            DataSource ds;
            Context ctx;
            Object obj;

            ctx = new InitialContext();
            obj = ctx.lookup(jndiName);
            ds = (DataSource) PortableRemoteObject.narrow(obj, DataSource.class);
            conn = ds.getConnection();
        }
        catch (NamingException e)
        {
            throw new DBException("Failed to get data source from JNDI directory: " + e.getMessage(), e);
        }
        catch (SQLException e)
        {
            throw new DBException("Unable to get connection from connection pool: " + e.getMessage(), e);
        }

我在wso2carbon.xml中遇到的错误是:

TID: [0] [AS] [2016-02-04 15:14:57,007] ERROR {POCDAO} -  A DBException occurred {POCDAO}
DBException: Unable to get connection from connection pool: Cannot create PoolableConnectionFactory (Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
 )
    at JNDIConnectionDescriptor.getConnection(JNDIConnectionDescriptor.java:159)
    at JNDIConnectionDescriptor.getConnectionRO(JNDIConnectionDescriptor.java:49)
    at ConnectionManager.getConnectionRO(ConnectionManager.java:179)
    at BaseDAO.getConnection(BaseDAO.java:66)
    at server.POCServer.savePocs(POCServer.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180)
    at org.apache.cxf.jaxws.JAXWSMethodInvoker.performInvocation(JAXWSMethodInvoker.java:66)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
    at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.invoke(AbstractJAXWSMethodInvoker.java:232)
    at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:85)
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:74)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$2.run(ServiceInvokerInterceptor.java:126)
    at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:131)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:251)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160)
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:180)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:293)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:212)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:268)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.wso2.carbon.statistics.webapp.RequestIntercepterValve.invoke(RequestIntercepterValve.java:43)
    at org.wso2.carbon.bam.webapp.stat.publisher.WebAppStatisticPublisherValve.invoke(WebAppStatisticPublisherValve.java:104)
    at org.wso2.carbon.tomcat.ext.valves.CompositeValve.continueInvocation(CompositeValve.java:178)
    at org.wso2.carbon.tomcat.ext.valves.CarbonTomcatValve$1.invoke(CarbonTomcatValve.java:47)
    at org.wso2.carbon.webapp.mgt.TenantLazyLoaderValve.invoke(TenantLazyLoaderValve.java:56)
    at org.wso2.carbon.tomcat.ext.valves.TomcatValveContainer.invokeValves(TomcatValveContainer.java:47)
    at org.wso2.carbon.tomcat.ext.valves.CompositeValve.invoke(CompositeValve.java:141)
    at org.wso2.carbon.tomcat.ext.valves.CarbonStuckThreadDetectionValve.invoke(CarbonStuckThreadDetectionValve.java:156)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.wso2.carbon.tomcat.ext.valves.CarbonContextCreatorValve.invoke(CarbonContextCreatorValve.java:52)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1653)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
 )
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at JNDIConnectionDescriptor.getConnection(JNDIConnectionDescriptor.java:97)
    ... 58 more
Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:743)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
    at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
    ... 61 more
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

    at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:275)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:264)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
    ... 68 more

0 个答案:

没有答案