Java WebLogic多连接

Java WebLogic多连接,java,web,connection,database-connection,weblogic12c,Java,Web,Connection,Database Connection,Weblogic12c,我的WebLogic服务器上有两个数据源,每个数据源访问不同的数据库。在我的客户端应用程序上,我有一些方法需要连接到第一个数据库,还有一些方法需要连接到第二个数据库但是当我运行它时,它只获取我执行的第一个方法的连接例如,如果我执行一个获得第一个数据库连接的方法,则只有访问该数据库的方法才能工作,我无法执行任何需要其他连接的方法监视选项卡,并验证两个DS都已启动并正在运行。。

我的WebLogic服务器上有两个数据源,每个数据源访问不同的数据库。
在我的客户端应用程序上,我有一些方法需要连接到第一个数据库,还有一些方法需要连接到第二个数据库
但是当我运行它时,它只获取我执行的第一个方法的连接
例如,如果我执行一个获得第一个数据库连接的方法,则只有访问该数据库的方法才能工作,我无法执行任何需要其他连接的方法<谁能帮我做这个吗?我正在使用WebLogic 12c

这是我的类,用于获取数据源:

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

}
下面是两个使用不同连接的方法示例:

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

在这两种情况下,您都使用
私有静态数据源KironMySql=null实例。为不同的DS提供单独的
数据源
对象

基本上,您在尝试获取
KironTabelaApp
DS连接时屏蔽了
KironLocal
DS

因此,您的更新代码如下所示:

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

}


这是我的数据源和监控选项卡

您是否已通过WL中的“监控”选项卡验证您的两个数据源都处于“运行”状态???@hagrawal只有一个连接正在运行。如何将两个连接都更改为running状态但是WL中的“KironLocal”和“KironTabelaApp”DS如何,它们是否都在运行?如果您希望与不同的数据库建立不同的连接,则需要为每个数据库提供不同的数据源。从应用程序中,获取与每个DS的连接(这意味着与数据库的连接),并使用它。不要使用相同的连接来连接2个不同的DS(这意味着连接到数据库)。如果您希望与不同的数据库建立不同的连接,则需要为每个数据库提供不同的数据源。从应用程序中,获取与每个DS的连接(这意味着与数据库的连接),并使用它。不要使用相同的连接连接到2个不同的DS(这意味着连接到数据库),因为它不起作用。。我认为可能是这样,但我仍然只有一个数据源在WL中运行这是因为在您的WL“KironTabelaApp”中,数据源没有运行。您需要转到“监视”选项卡并验证“KironTabelaApp”和“KironLocal”是否处于运行状态。首先,您需要解决WL问题,然后它将出现在应用程序代码中。。在您的WL运行2个必需的DS之前,不会有任何进展。您的WL中是否配置了其他DS??如果是,你开始了吗??请提供-WL的DS屏幕截图和WL的DS监控选项卡屏幕截图??就是这样。我告诉过你,你的第二个DS可能没有运行。现在,点击“KironTabelaApp”数据源。。转到“控制”选项卡,然后选择数据源并启动它。基本上你需要启动“KironTabelaApp”DS。完成后,再次转到数据源->监视选项卡,并验证两个DS都已启动并正在运行。。