java应用程序JNDI方式使用weblogic连接池的方法

来源:互联网 发布:mac vim 编辑:程序博客网 时间:2024/06/03 15:58

第一步准备工作:首先需要明确一点,要获取weblogic的连接就必须依赖weblogic相关jar,不是网上所说的weblogic.jar,如果你把weblogic.jar配置在你的classpath里面是不够的,因为会报出java类文件找不到的错误诸如[Caused by:weblogic.security.subject.AbstractSubject

java.lang.NoClassDefFoundError: weblogic/security/subject/AbstractSubject]之类的异常。那么我们怎么办呢,其实很简单只要进入到你webloigc_home路径下进入到server/lib目录,并且运行[jdk1.6 命令“java -jar wljarbuilder.jar”;jdk1.5命令“java -jar wljarbuilder.jar -profilewlfullclient5 ”],拷贝“wlfullclient.jar或者wlfullclient5.jar”到你的classpath环境下;
第二步编码工作:新建一个java工程,新建一个java类,命名为Connector.java;首先我们初始化获取数据库连接的环境,即初始化context;

Hashtable<String, String>ht=new Hashtable<String, String>();
ht.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);
ht.put(Context.PROVIDER_URL, PROVIDER_URL);
try {
long t1=System.currentTimeMillis();
_context=new InitialContext(ht);
long t2=System.currentTimeMillis();
System.out.println(
“initial the context cost:”+(t2-t1)+” millseconds!”);
} catch (NamingException e) {
System.out.println(“initial context failured withException:”+e.getMessage());
e.printStackTrace();
}

接下来从context上查找JNDI获取到Datasource对象:javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup(JNDI);
再就是从Datasource中得到java.sql.Connection对象了。ds.getConnection();

3 具体的示例代码请参考如下内容,笔者将源码完全贴出:

import java.sql.*;
import java.util.*;
import javax.naming.*;
/**
* <pre>
* to get the connections of weblogic connection pool by JNDI
* the required jars include
“wlfullclient.jar”,
* I know you will ask me how to get this jar? the following is the answer:
* first you must has installed the weblogic,WL_HOME means the weblogic setupdirectory.
* (a).Creating a wlfullclient.jar for JDK 1.6 client applications
* ===================================================================
*    1.Change directories to the server/lib directory.
*      cd WL_HOME/server/lib
*    2.Use the following command to create wlfullclient.jar inthe “server/lib” directory:
*      java -jar wljarbuilder.jar
*      this may take a few minutes ,just wait!
*    3.You can now copy and bundle the wlfullclient.jar withclient applications.
*    4.Add the wlfullclient.jar to the client application’s classpath.
* ===================================================================
* (b).Creating a wlfullclient5.jar for JDK 1.5 client applications
* ===================================================================
*    1. the same as (a).1
*    2. Use the following command to create wlfullclient.jar inthe server/lib directory:
*       java -jar wljarbuilder.jar -profilewlfullclient5
*    3. the same as (a).3
*    4. the same as (a).4
* ===================================================================
*
*
* </pre>
* @author ubuntu
*
*/
public class Connector {
/**
* the const variable of initial context Factory,this can’t modified!
*/
final static String INITIAL_CONTEXT_FACTORY=”weblogic.jndi.WLInitialContextFactory”;
/**
* the const variable of the t3 provider URl,this need to modified by your own !
*/
final static String PROVIDER_URL= “t3://10.254.0.9:7001″;

/**
* the const variable of the JNDI name ,this need to modified by your own !
*/
final static String JNDI=
”jdbc/powererp”;

/**
* the method that get Connecton from weblogic JNDI
* @return the Connection Object
*/
public Connection getConnection(){
Context ctx=null;
Connection con=null;
try{
long t1=System.currentTimeMillis();
ctx=_context;
long t2=System.currentTimeMillis();
//System.out.println(
“initial the context cost:”+(t2-t1)+” millseconds!”);
javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup(JNDI);
long t3=System.currentTimeMillis();
System.out.println(“get the datasource cost:”+(t3-t2)+” millseconds!”);
con=ds.getConnection();
long t4=System.currentTimeMillis();
System.out.println(“get the connection cost:”+(t4-t3)+” millseconds!”);

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

static Context _context=null;

static{
Hashtable<String, String> ht=new Hashtable<String, String>();
ht.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);
ht.put(Context.PROVIDER_URL, PROVIDER_URL);
try {
long t1=System.currentTimeMillis();
_context=new InitialContext(ht);
long t2=System.currentTimeMillis();
System.out.println(
“initial the context cost:”+(t2-t1)+” millseconds!”);
} catch (NamingException e) {
System.out.println(“initial context failured withException:”+e.getMessage());
e.printStackTrace();
}
}

/**
* the method that test whether the connection is correct got
* @param con
*/
private void testconn(Connection con){
ResultSet rs=null;
Statement stmt=null;
String sql=
”select sysdate from dual”;
if(con==null)throw new RuntimeException(“the connectionis null.please check!”);
try {
stmt=con.prepareStatement(sql);
rs=stmt.executeQuery(sql);
if(rs!=null&&rs.next()){
String sysdate=rs.getString(1);
System.out.println(“the oracle db sysdate is :”+sysdate);
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“the excepiton occured:”+e.getMessage());

}finally{
try{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
}
catch(Exception e){}
}

}

/**
* test the connection by jndi
*/
private void invokeTestDbConnectionByJndi(){
Connection con=null;
try{
long tstart=System.currentTimeMillis();
con=    getConnection();
long tend=System.currentTimeMillis();
System.out.println(
“execute the method getConnection() cost:”+(tend-tstart)+” millseconds!”);
testconn(con);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(“when executeinvokeTestDbConnectionByJndi(),” +
“the excepiton occured:”+e.getMessage());
}finally{
try{
if(con!=null)con.close();
}
catch(Exception e){}
}

}

/**
* the main method
* @param args args allow null
*/
public static void main(String[] args) {
Connector cct=new Connector();
cct.invokeTestDbConnectionByJndi();
cct.invokeTestDbConnectionByJndi();
}

//—————-thefollowing his -the test result :———————//
/**
*
* initial the context cost:5666 millseconds!
* get the datasource cost:225 millseconds!
* get the connection cost:968 millseconds!
* execute the method getConnection() cost:1193 millseconds!
* the oracle db sysdate is :2010-08-24 13:31:50.0
* get the datasource cost:4 millseconds!
* get the connection cost:33 millseconds!
* execute the method getConnection() cost:37 millseconds!
* the oracle db sysdate is :2010-08-24 13:31:51.0
*
*/
}

 

原创粉丝点击