henrique romao henrique romao - 3 years ago 73
Java Question

WebLogic multiple Connections

I have two DataSources on my WebLogic Server, each accessing different DBs.
On my client application, I have some methods that need to connect to the first DB, and others that need to connect to the second one.
But when I run it, it's only getting the connection of the first method I execute.
For example, if I execute a method that gets the connection of the firs DB, only the methods that access this DB will work, I can't execute any method that needs the other connection.
Can somebody help me with this? I'm using WebLogic 12c

This is my class that get the Data Sources:

package com.henrique.dao;

import java.sql.Connection;

import javax.naming.*;
import javax.sql.*;

public class KironMySql {

private static DataSource KironMySql = null;
private static Context context = null;

public static DataSource KironMySqlConn() throws Exception{
if (KironMySql != null) {
return KironMySql;
}
try{
if(KironMySql == null){
context = new InitialContext();
KironMySql = (DataSource) context.lookup("KironLocal");
}
}catch(Exception e){
e.getMessage();
}
return KironMySql;
}

public static DataSource KironMySqlConnIp() throws Exception{
if (KironMySql != null) {
return KironMySql;
}
try{
if(KironMySql == null){
context = new InitialContext();
KironMySql = (DataSource) context.lookup("KironTabelaApp");
}
}catch(Exception e){
e.getMessage();
}
return KironMySql;
}

public static Connection KironConnection(){
Connection conn = null;
try{
conn = KironMySqlConn().getConnection();
return conn;
}catch(Exception e){
e.getMessage();
}
return conn;
}

public static Connection KironConnectionIp(){
Connection conn = null;
try{
conn = KironMySqlConnIp().getConnection();
return conn;
}catch(Exception e){
e.getMessage();
}
return conn;
}

}


And here are two examples of methods that use different connections:

public JSONArray Login(String usu_login, String usu_senha) throws Exception{
PreparedStatement query = null;
Connection conn = null;
ToJson converter = new ToJson();
JSONArray json = new JSONArray();

try{
conn = KironMySql.KironConnection();
query = conn.prepareStatement("select usu_nome from usuario where usu_login = ? and usu_senha = ?");
query.setString(1, usu_login);
query.setString(2, usu_senha);
ResultSet rs = query.executeQuery();
json = converter.toJSONArray(rs);
query.close();
}catch(Exception e){
e.printStackTrace();
return json;
}finally{
if(conn != null) conn.close();
}
return json;
}

public JSONArray getIp(String emp_codigo) throws Exception{
PreparedStatement query = null;
Connection conn = null;
ToJson converter = new ToJson();
JSONArray json = new JSONArray();

try{
conn = KironMySql.KironConnectionIp();
query = conn.prepareStatement("select con_ip from conexaoapp where emp_codigo = ?");
query.setString(1, emp_codigo);
ResultSet rs = query.executeQuery();
json = converter.toJSONArray(rs);
query.close();
}catch(Exception e){
e.printStackTrace();
return json;
}finally{
if(conn != null) conn.close();
}
return json;
}

Answer Source

In both the case you are using private static DataSource KironMySql = null; instance. Have separate DataSource object for different DS.

Essentially you were masking KironLocal DS when trying to get KironTabelaApp DS connection.

So, your updated code will look like as below:

package com.henrique.dao;

import java.sql.Connection;

import javax.naming.*;
import javax.sql.*;

public class KironMySql {

    private static DataSource KironMySql = null;
    private static DataSource KironMySqlIp = null;  //This is new line for code fix, and using "KironMySqlIp" instance later in the code where connection with "KironTabelaApp" data source is needed.
    private static Context context = null;

    public static DataSource KironMySqlConn() throws Exception{
        if (KironMySql != null) {
            return KironMySql;
        }
        try{
            if(KironMySql == null){
                context = new InitialContext();
                KironMySql = (DataSource) context.lookup("KironLocal");
            }           
        }catch(Exception e){
            e.getMessage();
        }
        return KironMySql;
    }

    public static DataSource KironMySqlConnIp() throws Exception{
        if (KironMySqlIp != null) {
            return KironMySqlIp;
        }
        try{
            if(KironMySqlIp == null){
                context = new InitialContext();
                KironMySqlIp = (DataSource) context.lookup("KironTabelaApp");
            }           
        }catch(Exception e){
            e.getMessage();
        }
        return KironMySqlIp;
    }

    public static Connection KironConnection(){
        Connection conn = null;
        try{
            conn = KironMySqlConn().getConnection();
            return conn;
        }catch(Exception e){
            e.getMessage();
        }
        return conn;
    }

    public static Connection KironConnectionIp(){
        Connection conn = null;
        try{
            conn = KironMySqlConnIp().getConnection();
            return conn;
        }catch(Exception e){
            e.getMessage();
        }
        return conn;
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download