我需要做些什么来纠正我的WebLogic Universal Pooled连接数据源JNDI绑定错误?

时间:2015-04-16 20:05:07

标签: java mysql java-ee jdbc

调用简单地建立我所称的JNDI未绑定WebLogic通用连接池(UCP)数据源 jndiUnboundDataSource() ,并且能够创建&安培;返回一个可用的数据库连接。

jndiBindDataSource() 方法引发以下异常:

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

jndiBindDataSource() 方法正在尝试创建UCP数据源,然后将其绑定到JNDI上下文...意图是 < em> 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

1 个答案:

答案 0 :(得分:0)

这看起来像是在尝试定义远程数据源。

出于多种原因,你不能这样做,其中最重要的原因是javax.sql.DataSource没有扩展java.io.Serializable

您无需创建自己的数据源并将其绑定。您可以使用WebLogic控制台对其进行定义,并在此时为其指定JNDI名称。然后WLS负责创建数据源并为您绑定它。所有完整堆栈的Java EE服务器(以及Tomcat)都是这样做的。