【问题标题】:Call JDBC and UCP connection from OSGI bundle从 OSGI 包调用 JDBC 和 UCP 连接
【发布时间】:2013-12-19 20:42:18
【问题描述】:

我有一个关于将初始化 JDBC 和 UCP 连接调用到 byndle 激活器的具体问题。我创建了这个激活器:

public class Activator implements BundleActivator
{

    @Override
    public void start(BundleContext bc) throws Exception
    {

        testOracleUCP();
        System.out.println("Started module");
    }

    @Override
    public void stop(BundleContext bc) throws Exception
    {

    }

    public void testOracleUCP() throws Exception
    {
        System.out.println("Test Oracle UCP");
        Connection conn = null;
        try
        {
            conn = OracleDS_UCP.getConnection();
            OracleDS_UCP.getPoolDataSourceStatus();
        }
        catch (SQLException e)
        {
            throw e;
        }
        finally
        {
            if (conn != null)
                conn.close();
        }

    }
}

我使用这个 Java 类来调用初始化连接:

public class OracleDS_UCP
{
    protected static final PoolDataSource pds;

    static
    {
        pds = initPoolDataSource();
    }

    public static Connection getConnection() throws SQLException
    {
        if (pds == null)
            return null;
        Connection conn = pds.getConnection();
        conn.setAutoCommit(false);
        return conn;
    }

    private static PoolDataSource initPoolDataSource()
    {
        try
        {
            System.out.println("\nStarting Oracle UCP Connection");
            PoolDataSource pool = PoolDataSourceFactory.getPoolDataSource();
            pool.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");

            pool.setURL("jdbc:oracle:thin:@146.185.142.243:1521:XE");
            pool.setUser("SYSTEM");
            pool.setPassword("4r3e2w1q");

            //Setting pool properties (can be retrieved from config file)
            pool.setMaxStatements(10); // the maximum number of statements that may be pooled or cached on a connection.
            pool.setInitialPoolSize(2);
            pool.setMinPoolSize(1);
            pool.setMaxPoolSize(50);
            pool.setLoginTimeout(60); // one minute
            pool.setConnectionWaitTimeout(60); // one minute
            pool.setAbandonedConnectionTimeout(30 * 60); // thirty minutes
            pool.setMaxIdleTime(60 * 60); // one hour and kill inactive or idle connections
            pool.setInactiveConnectionTimeout(60 * 60); // one hour and kill inactive or idle connections
            pool.setConnectionWaitTimeout(0); // do not wait for a used connection to be released by a client.
            pool.setConnectionHarvestTriggerCount(40);
            pool.setConnectionHarvestMaxCount(10);

            return pool;
        }
        catch (SQLException ex)
        {
            Logger.getLogger(OracleDS_UCP.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }
}

我将 JDBC 驱动程序和 ucp 驱动程序嵌入到包中:

<Embed-Dependency>ucp,ojdbc6;scope=compile|runtime</Embed-Dependency> 

这是捆绑的清单文件:

Manifest-Version: 1.0
Bnd-LastModified: 1387484566727
Build-Jdk: 1.8.0-ea
Built-By: developer
Bundle-Activator: org.project.osgi.SQLEngine.activator.Activator
Bundle-ClassPath: .,junit-4.11.jar,org.osgi.core-1.4.0.jar,org.osgi.comp
 endium-5.0.0.jar,ojdbc6-11.2.0.3.jar,ucp-11.2.0.3.jar,SQL_Exchanges-1.0
 .jar,Agents_Manager_api-2.0.jar
Bundle-ManifestVersion: 2
Bundle-Name: SQL_Engine
Bundle-SymbolicName: SQL_Engine
Bundle-Vendor: Corporation Name
Bundle-Version: 1.0.0
Created-By: Apache Maven Bundle Plugin
Embed-Dependency: *;scope=compile|runtime
Embedded-Artifacts: junit-4.11.jar;g="junit";a="junit";v="4.11",org.osgi
 .core-1.4.0.jar;g="org.apache.felix";a="org.osgi.core";v="1.4.0",org.os
 gi.compendium-5.0.0.jar;g="org.osgi";a="org.osgi.compendium";v="5.0.0",
 ojdbc6-11.2.0.3.jar;g="com.oracle";a="ojdbc6";v="11.2.0.3",ucp-11.2.0.3
 .jar;g="com.oracle";a="ucp";v="11.2.0.3",SQL_Exchanges-1.0.jar;g="SQL_E
 xchanges";a="SQL_Exchanges";v="1.0",Agents_Manager_api-2.0.jar;g="DX-57
 -Kernel";a="Agents_Manager_api";v="2.0"
Export-Package: org.project.osgi.SQLEngine.api;version="1.0.0"
Import-Package: org.osgi.framework;version="[1.5,2)",javax.sql.DataSourc
 e
Tool: Bnd-2.1.0.20130426-122213

我得到这个错误:

Caused by: java.lang.ClassNotFoundException: javax.sql.DataSource not found by S
QL_Engine [1147]

知道如何解决这个问题吗?

【问题讨论】:

    标签: java osgi apache-felix osgi-bundle


    【解决方案1】:

    问题是在Import-Package: 标头中提到了javax.sql.DataSource,但这实际上是一个类而不是一个包。应该是javax.sql。这不是 maven 插件做的吧?

    【讨论】:

    • Import-Package 指定包使用的 Java 包,因此 OSGi 框架知道如何在运行时连接包。因此,应在此处提及捆绑使用且捆绑未提供的所有包(java.* 除外)。通常,maven bnd 插件会自动生成这些导入,并且由于您的 MANIFEST 表明它是由 bnd/maven 生成的,所以我得到了您使用 maven 的印象。但我绝对不相信插件添加了 javax.sql.DataSource(当然,除非你有一个具有该名称的包,但这不太可能)。
    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 2017-05-28
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 2016-04-06
    相关资源
    最近更新 更多