javax.naming.NameNotFoundException:Name" JDBC"不受此上下文约束

时间:2015-07-23 09:45:06

标签: java

我正在尝试使用context.xml文件

访问tomcat中的JNDI数据源

但是我遇到了一些问题

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
        at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:168)

这是我的代码: -

context.xml中

<?xml version="1.0" encoding="UTF-8"?> 

<Context> 

<Resource name="jdbc/testdb" auth="Container" 
       type="com.mchange.v2.c3p0.ComboPooledDataSource" 
        user="ngridlive" 
        password="gregory" 
        driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
        jdbcUrl="jdbc:sqlserver://192.138.41.153:1433;databaseName=TestDB" 
        factory="org.apache.naming.factory.BeanFactory"     
        maxPoolSize="100"
        minPoolSize="20"
        acquireIncrement="3"
        maxIdleTime="7200"
        maxIdleTimeExcessConnections="300"
        automaticTestTable="ConnTest"
        testConnectionOnCheckin="true"
        idleConnectionTestPeriod="1800"
        acquireRetryAttempts="10"
        acquireRetryDelay="2000"/>  
</Context> 
连接类

中的

public Connection getConnection(String clientId,String databaseName)

抛出异常{

    Connection con = null;

    try{

        InitialContext context = null;
        String datasourceName = null;
        DataSource ds = null;
        context = new InitialContext();

        datasourceName = clientId + databaseName;

        ds = (DataSource) context.lookup("java:comp/env/jdbc/" + datasourceName);
        con = ds.getConnection();   
    }catch(Exception e){
        e.printStackTrace();
    }
    return con;
}

0 个答案:

没有答案