mercredi 15 juillet 2015

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;
    }   




Aucun commentaire:

Enregistrer un commentaire