How to Connect to WebLogic DataSource from a Java Client?

Some time ago, one of my customers had issues connecting from a client to a WebLogic Datasource.
I provided the sample code to try the functionality, here are the steps:

1. Save the code below as DataSourceClient.java in /myfolder

import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.*;
import java.io.*;
import oracle.jdbc.OracleConnection;

public class DataSourceClient {
  public static void main(String [] args) {
    OracleConnection conn=null;
    javax.sql.DataSource ds=null;
    Hashtable env = new Hashtable();
    env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
    env.put(Context.PROVIDER_URL, "t3://localhost:7001");

    try{
      Context context=new InitialContext( env );
      //you will need to have create a Data Source with JNDI name testDS
      ds=(javax.sql.DataSource) context.lookup ("testDS");
      conn=(OracleConnection) ds.getConnection();
      java.util.Properties prop = new java.util.Properties();
      System.out.println("Connection object details : "+conn);
      conn.close();
    }catch(Exception ex){
      //handle the exception
      ex.printStackTrace();
    }
  }
}


2. Modify PROVIDER_URL if needed. The value is set to t3://localhost:7001
3. Add to WebLogic Server JAVA_OPTIONS -Dweblogic.jdbc.remoteEnabled=true
4. Start WebLogic Server
5. Open a UNIX console. In the same console run:
    i. cd <domain>/bin
    ii. . ./setDomainEnv.sh (Notice the '.' before './setDomainEnv.sh')
    iii. CLASSPATH="/myfolder:${CLASSPATH}"
    iv   cd /myfolder
    v.   javac DataSourceClient.java
    vi.  java DataSourceClient


If you succeeed, you will see something like this:

Connection object details : weblogic.jdbc.rmi.SerialConnection_weblogic_jdbc_rmi_internal_ConnectionImpl_weblogic_jdbc_wrapper_JTAConnection_weblogic_jdbc_wrapper_XAConnection_oracle_jdbc_driver_Logic
alConnection_1036_WLStub@1       

This means, you sucessfully connected to your WLS Datasource.

Do you need further help?

Please check WebLogic Documentation:
http://docs.oracle.com/cd/E23943_01/web.1111/e13726/programming.htm

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About


My name is Luz Mestre. I work as Principal Technical Support Engineer at Oracle Support. I'll post here the most interesting challenges I have in my daily work. The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
8
9
10
11
12
13
14
15
16
17
18
19
20
22
23
24
25
26
27
28
29
30
   
       
Today