簡體   English   中英

我該怎么做才能糾正WebLogic通用池連接數據源JNDI綁定錯誤?

[英]What do I need to do to correct my WebLogic Universal Pooled Connection Data Source JNDI binding error?

僅建立我所指的JN​​DI未綁定WebLogic通用連接池(UCP)數據源jndiUnboundDataSource()的調用即可工作,並且能夠創建並返回可用的數據庫連接。

jndiBindDataSource()方法引發以下異常:

意外的異常:java.rmi.RemoteException:oracle.ucp.jdbc.PoolDataSourceImpl $ 23

jndiBindDataSource()方法正在嘗試創建UCP數據源,然后將其綁定到JNDI上下文……目的是jndiBoundDataSource()方法將能夠從JNDI上下文中檢索綁定的UCP數據源。 此時,我還沒有測試jndiBoundDataSource(),因為jndiBindDataSource()失敗了。

WebLogic 12.1.3-通用連接池ConnectionClassFactory:

com.mysql.jdbc.jdbc2.optional.MysqlDataSource

您應該能夠從以下來源重新創建錯誤:

package com.corporate.experiments;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.ValidConnection;

public class WebLogicUniversalConnectionPool extends HttpServlet
{
    @SuppressWarnings("oracle.jdeveloper.java.serialversionuid-stale") private static final long serialVersionUID = 1L;

    // http://docs.oracle.com/middleware/1213/wls/JDBCP/ds_annotation.htm#BABEDEJJ
    // http://docs.oracle.com/middleware/1213/wls/JDBCP/ds_annotation.htm#BABDBHCD

    /**
     *  The name element uniquely identifies a DataSource and is registered with JNDI.
     *  The value specified in the name element begins with a namespace scope.
     *
     *  Java EE Data Source Naming Scopes:
     *
     *  java:comp - Names in this namespace have component visibility.
     *  java:module - Names in this namespace are shared by all components in a module, for example, the EJB components defined in an a ejb-jar.xml file.
     *  java:app - Names in this namespace are shared by all components and modules in an application, for example, the application-client, web, and EJB components in an .ear file.
     *  java:global - Names in this namespace are shared by all the applications in the server.
     *
     *  Java EE Data Source Naming Conventions:
     *
     *  Component visibility:
     *  ---------------------
     *  appname @ modulename @ componentname @ dsname
     *
     *  Module visibility:
     *  ------------------
     *  appname @ modulename @ dsname
     *
     *  Application visibility:
     *  -----------------------
     *  appname @ dsname
     *
     *  Global visibility:
     *  ------------------
     *  dsname
     */

    private static final String DATA_SOURCE_NAME = "app/content";
    private static final String DATA_SOURCE_NAMESPACE = "java:" + DATA_SOURCE_NAME;

    /**
     * @param request
     * @param response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    {
        // http://docs.oracle.com/database/121/JJUCP/connect.htm#JJUCP8139

        try
        {
            jndiUnboundDataSource();
        }
        catch (Exception exception)
        {
            return; // place break point here
        }

        // create a context bound connection pool data source

        try
        {
            jndiBindDataSource(); // Java Naming and Directory Interface (JNDI)
        }
        catch (Exception exception)
        {
            return; // place break point here
        }

        // get a connection from the bound connection pool data source

        try
        {
            jndiBoundDataSource(); // Java Naming and Directory Interface (JNDI)
        }
        catch (Exception exception)
        {
            return; // place break point here
        }

        return; // place break point here
    }

    // create a context unbound connection pool data source and get a connection

    private DataSource jndiUnboundDataSource() throws SQLException
    {
        DataSource dataSource = createDataSource();

        validateDataSource(dataSource);

        return dataSource;
    }

    // create a context and bind a connection pool data source

    private DataSource jndiBindDataSource() throws NamingException, SQLException
    {
        InitialContext context = createInitialContext();

        DataSource dataSource = createDataSource();

        validateDataSource(dataSource);

        // Unexpected exception: java.rmi.RemoteException: oracle.ucp.jdbc.PoolDataSourceImpl$23;

        context.bind(DATA_SOURCE_NAME, dataSource);

        return dataSource;
    }

    // create a context and look up a connection pool data source

    private DataSource jndiBoundDataSource() throws NamingException, SQLException
    {
        InitialContext context = initialContext();

        PoolDataSource poolDataSource = (PoolDataSource) context.lookup(DATA_SOURCE_NAME);

        validateDataSource(poolDataSource);

        return poolDataSource;
    }

    private InitialContext initialContext() throws NamingException
    {
        Hashtable<String, String> environment = new Hashtable<String, String> ();

        environment.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        environment.put(Context.PROVIDER_URL, "t3://localhost:7101");
        environment.put(Context.SECURITY_AUTHENTICATION, "Container");
        environment.put(Context.SECURITY_PRINCIPAL, "development");
        environment.put(Context.SECURITY_CREDENTIALS, "development");

        return new InitialContext(environment);
    }

    private InitialContext createInitialContext() throws NamingException
    {
        InitialContext initialContext = initialContext();

        initialContext.createSubcontext("corporate");

        return initialContext;
    }

    private DataSource createDataSource() throws SQLException
    {
        PoolDataSource poolDataSource = PoolDataSourceFactory.getPoolDataSource();

        poolDataSource.setConnectionFactoryClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");

        poolDataSource.setURL("jdbc:mysql://localhost:3306/metadata");
        poolDataSource.setUser("root");
        poolDataSource.setPassword("root");

        return poolDataSource;
    }

    private void validateDataSource(DataSource dataSource) throws SQLException
    {
        Connection connection = dataSource.getConnection();

        validateConnection(connection);

        connection.close();
    }

    private void validateConnection(Connection connection) throws SQLException
    {       
        if (null == connection)
        {
            throw new SQLException("Null connection: servlet data source namespace \"" + DATA_SOURCE_NAMESPACE + "\"");
        }

        if (!((ValidConnection) connection).isValid())
        {
            throw new SQLException("Invalid connection: servlet data source namespace \"" + DATA_SOURCE_NAMESPACE + "\"");
        }
    }    
}

這是例外:

        Unexpected exception: java.rmi.RemoteException: oracle.ucp.jdbc.PoolDataSourceImpl$23; nested exception is:

        java.io.NotSerializableException: oracle.ucp.jdbc.PoolDataSourceImpl$23
        at weblogic.jndi.internal.WLEventContextImpl.copyObject(WLEventContextImpl.java:400)
        at weblogic.jndi.internal.WLEventContextImpl.bind(WLEventContextImpl.java:279)
        at javax.naming.InitialContext.bind(InitialContext.java:419)
        at com.corporate.experiments.WebLogicUniversalConnectionPool.jndiBindDataSource(WebLogicUniversalConnectionPool.java:123)
        at com.corporate.experiments.WebLogicUniversalConnectionPool.doGet(WebLogicUniversalConnectionPool.java:81)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
        at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
        at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
        at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
        at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)
        at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
        at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:137)
        at java.security.AccessController.doPrivileged(Native Method)
        at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:315)
        at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:460)
        at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:120)
        at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:217)
        at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:81)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
        at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:220)
        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3436)
        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
        at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
        at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)
        at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)
        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)
        at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)
Caused by: java.io.NotSerializableException: oracle.ucp.jdbc.PoolDataSourceImpl$23
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
        at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
        at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:440)
        at oracle.ucp.jdbc.PoolDataSourceImpl.writeObject(PoolDataSourceImpl.java:3015)
        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 java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
        at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
        at weblogic.rmi.extensions.server.CBVOutputStream.writeObject(CBVOutputStream.java:80)
        at weblogic.jndi.internal.JNDIHelper.copyObject(JNDIHelper.java:39)
        at weblogic.jndi.WLSJNDIEnvironmentImpl.copyObject(WLSJNDIEnvironmentImpl.java:78)
        at weblogic.jndi.internal.WLEventContextImpl.copyObject(WLEventContextImpl.java:395)
        ... 34 more

看起來您正在嘗試定義遠程數據源。

由於多種原因,您不能執行此操作,其中最重要的原因是javax.sql.DataSource不擴展java.io.Serializable

您無需創建自己的數據源並將其綁定。 您可以使用WebLogic控制台對其進行定義,然后為其指定一個JNDI名稱。 然后WLS會負責創建數據源並為您綁定它。 所有全棧Java EE服務器(以及Tomcat)都采用這種方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM