尝试绑定dbcp的BasicDataSource时,我得到了javax.naming.OperationNotSupportedException

时间:2014-06-19 19:42:27

标签: java jndi apache-commons-dbcp

我正在使用Apache的公共DBCP 1.4版本的Jar。我通过fscontex跟踪绑定BasicDataSource的JNDI示例。如以下链接http://commons.apache.org/proper/commons-dbcp/guide/jndi-howto.html提供的代码所示。

我写了类似的独立代码,但我收到了以下错误 " javax.naming.OperationNotSupportedException:只能绑定References或Referenceable对象" 。根据我的kmowledge,任何想要引用的对象都必须实现javax.naming.Referenceable接口并定义getReference方法。我不确定BasicDataSource是否正在这样做?

这是我用过的代码。

import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;

public class DBCPTest {

public static void main(String[] args) {
    try {
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.fscontext.RefFSContextFactory");
        System.setProperty(Context.PROVIDER_URL, "file:///F:/JNDI/");
        InitialContext ic = new InitialContext();

        // Construct BasicDataSource
        BasicDataSource bds = new BasicDataSource();
        bds.setDriverClassName("org.apache.commons.dbcp2.TesterDriver");
        bds.setUrl("jdbc:apache:commons:testdriver");
        bds.setUsername("username");
        bds.setPassword("password");

        ic.rebind("jdbc/basic", bds);

        // Use
        InitialContext ic2 = new InitialContext();
        DataSource ds = (DataSource) ic2.lookup("jdbc/basic");

        Connection conn = ds.getConnection();
        conn.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

以下是我在课程中的JAR列表

  • commons-dbcp-1.4.jar
  • commons-ppol-1.6.jar
  • fscontext.jar
  • providerutil.jar
  • jndi.jar

对于解决此问题或指出我做错了什么的任何见解都表示赞赏。

由于 CM

1 个答案:

答案 0 :(得分:0)

在浏览网页后,我确实找到了一种通过基于文件的上下文绑定BasicDataSource的方法。我使用了以下链接提供的示例

http://www.massapi.com/class/javax/naming/StringRefAddr.java.html

System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.fscontext.RefFSContextFactory");
    System.setProperty(Context.PROVIDER_URL, "file:///F:/JNDI/");

    Reference ref = new Reference("javax.sql.DataSource","org.apache.commons.dbcp.BasicDataSourceFactory", null);
    ref.add(new StringRefAddr("driverClassName","com.ibm.db2.jcc.DB2Driver");
    ref.add(new StringRefAddr("url","jdbc:db2://myhost.example.com:port/dbname");
    ref.add(new StringRefAddr("password", "SomePassord");
    ref.add(new StringRefAddr("username", "myUser");

    ref.add(new StringRefAddr("maxActive","100"));
    ref.add(new StringRefAddr("maxWait", "10000"));
    ref.add(new StringRefAddr("maxIdle", "10"));
    ref.add(new StringRefAddr("minIdle", "5"));

    ref.add(new StringRefAddr("testOnBorrow", "true"));
    ref.add(new StringRefAddr("testOnReturn", "false"));
    ref.add(new StringRefAddr("testWhileIdle","true"));
    ref.add(new StringRefAddr("validationQuery","SELECT 1"));

    ref.add(new StringRefAddr("timeBetweenEvictionRunsMillis",Integer.toString(10*60*1000)));
    ref.add(new StringRefAddr("minEvictableIdleTimeMillis",Integer.toString(2*60*1000)));
    ref.add(new StringRefAddr("numTestsPerEvictionRun","10"));

    ref.add(new StringRefAddr("removeAbandoned", "true"));
    ref.add(new StringRefAddr("removeAbandonedTimeout", Integer.toString(30*60)));
    ref.add(new StringRefAddr("logAbandoned", "true"));

    Context ctx = new InitialContext();
    ctx.rebind("jdbc/MyDataSource",ref)
    ctx.close();