1

SQL Server を使用するのはこれが初めてです。以前は MySQL で作業していて、Tomcat で db 接続プールを正常に作成していました。同じコードを使用して、SQL サーバーの接続プールを作成しようとしています。どこが間違っているのか教えてください。

Windows 認証としてログインしようとしています。Tomcat の bin ディレクトリに sqljdbc_auth.dll をコピーしました。

以下は、META-INF の下の context.xml ファイルに記述したコードです。

Context antiJARLocking="true" path="">
    <Resource 
    auth="Container" 
    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
    logAbandoned="true"
    maxActive="100"
    maxIdle="30" 
    maxWait="1000" 
    name="jdbc/mydb" 
    removeAbandoned="true"
    removeAbandonedTimeout="60"
    type="javax.sql.DataSource" 
    url="jdbc:sqlserver://localhost;integratedSecurity=true;" />
</Context>

以下は、接続を提供している Java クラスです。

public class ConnectionPool {
private static ConnectionPool pool=null;
private static DataSource dataSource = null;

public synchronized static ConnectionPool getInstance(){
    if (pool == null){
        pool = new ConnectionPool();
    }
    return pool;
}

private ConnectionPool(){
    try{
        InitialContext ic = new InitialContext();
        dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/mydb");
    }
    catch(Exception e){
        System.out.println(e);
    }
}


public Connection getConnection(){
    try{
        return dataSource.getConnection();
    }
    catch (SQLException sqle){
        System.err.println(sqle);
        return null;
    }
}

public void freeConnection(Connection c){
    try{
        c.close();
    }
    catch (SQLException sqle){
        System.err.println(sqle);
    }
}
}

電話をかけてみるとgetConnection()、次の例外が発生しました。 javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial Exception in thread "main" java.lang.NullPointerException at ConnectionPool.getConnection(ConnectionPool.java:39)

4

0 に答える 0