这是代码:
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;public class WLTestPool{ public static void main(String args[]){
String dsName = "MyDataSource";
               
DataSource ds = null;
Context ctx = null;
Connection myConn = null;
try{
ctx = getInitialContext();
//查找JNDI名字为dsName的数据源
//System.out.println("get Context successfully");
ds = (DataSource)ctx.lookup(dsName);
}catch(Exception E){
System.out.println("Init DataSource Error:"+E);
}
try{
if (ds!=null){
myConn = ds.getConnection();
    myConn.close();
}
}catch(Exception E){
System.out.println("Init Connection Error:"+E);
} }
    private static Context getInitialContext() throws Exception{
String url = "t3://localhost:7001";
String user = "weblogic";
String password = "dltstudy";
Properties properties = null;
try{
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
             properties.put(Context.PROVIDER_URL,url);
             properties.put(Context.SECURITY_PRINCIPAL,user);
             properties.put(Context.SECURITY_CREDENTIALS,password);
             //System.out.println("new proerties");
return new InitialContext(properties);
}catch(Exception e){
   throw e;
}
}
}

解决方案 »

  1.   

    myConn = ds.getConnection();
    是这句话发生了错误,取不连接。但在Weblogic中是有的,而且数据库中也能够看到连接数目。
    高手们,问题出在哪里?
    不够可以加分。
      

  2.   

    Weblogic中抱如下错误:
    <2004-9-13 下午02时41分05秒 CST> <Warning> <JDBC> <BEA-001074> <A JDBC pool conn
    ection leak was detected. A connection leak occurs when a connection obtained fr
    om the pool was not closed explicitly by calling close() and then was disposed b
    y the garbage collector and returned to the connection pool. The following stack
     trace at create shows where the leaked connection was created.  [Null exception
     passed, creating stack trace for offending caller]
            at weblogic.utils.StackTraceUtils.throwable2StackTrace(StackTraceUtils.j
    ava:28)
            at weblogic.jdbc.wrapper.PoolConnection.finalizeInternal(PoolConnection.
    java:84)
            at weblogic.jdbc.wrapper.PoolConnection_com_borland_datastore_jdbc_RConn
    ection.finalize(Unknown Source)
            at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
            at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
            at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
            at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
    >
      

  3.   

    " ds = (DataSource)ctx.lookup(dsName);"这是个远程调用,不能这样直接cast
      

  4.   

    ctx = getInitialContext();
    改为
    ctx = new InitialContext();
    试试
      

  5.   

    重启好几次了。不行
    ctx = new InitialContext();
    改为这个也不行。如下错误:
    Init DataSource Error:javax.naming.NoInitialContextException: Need to specify cl
    ass name in environment or system property, or as an applet parameter, or in an
    application resource file:  java.naming.factory.initial
      

  6.   

    我也碰上这个问题了,Weblogic8.1,但同样的代码连Weblogic7的没问题.
      

  7.   

    是不是用的orcale9?文档上有一段这样的描述:
    This situation occurs because on server restart, WebLogic Server calls XA.recover() to recover any pending transactions. With the 9.2.0.3 or earlier version of the Oracle Thin driver, the Oracle DBMS opens a local transaction to complete the transaction recovery work, but the local transaction is never closed. When the connection used to recover transactions is returned to the connection pool and is then reused by an application, the local transaction is still present. With the first operation on the connection, an XAER_PROTO error is thrown. (If TestConnsOnReserve is set to true, the connection test is the first operation on the connection.) WebLogic Server then attempts to unregister the connection with the resource and waits a fixed amount of time for all transaction work on the resource to complete. This may appear as a hang.