【问题标题】:WebLogic multiple ConnectionsWebLogic 多个连接
【发布时间】:2015-07-15 14:41:07
【问题描述】:

我的 WebLogic Server 上有两个数据源,每个都访问不同的数据库。
在我的客户端应用程序上,我有一些方法需要先连接到 DB,以及其他需要连接到 second 的。
但是当我运行它时,它只会获得我执行的第一个方法的连接。
例如,如果我执行一个获取第一个 DB 连接的方法,只有访问该 DB 的方法才能工作,我无法执行任何需要另一个连接的方法。
有人可以帮我吗?我正在使用 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;
    }   

【问题讨论】:

  • 您是否从 WL 的“监控”选项卡中验证了您的两个数据源都处于“运行”状态??
  • @hagrawal 只有一个连接在运行。如何将两个连接都更改为运行状态
  • 但是 WL 中的 "KironLocal" 和 "KironTabelaApp" DS 是不是都在运行??
  • 如果你想与不同的数据库建立不同的连接,那么你需要为每个数据库有不同的数据源。并从应用程序中获取与每个 DS 的连接(即与数据库的连接),并使用它。不要使用同一个连接来连接 2 个不同的 DS(即与数据库的连接)

标签: java web connection database-connection weblogic12c


【解决方案1】:

在这两种情况下,您都在使用 private static DataSource KironMySql = null; 实例。对不同的 DS 有单独的 DataSource 对象。

基本上你在尝试获得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;
    }

}

【讨论】:

  • 如果你想与不同的数据库建立不同的连接,那么你需要为每个数据库有不同的数据源。并从应用程序中获取与每个 DS 的连接(即与数据库的连接),并使用它。不要使用同一个连接来连接 2 个不同的 DS(即与数据库的连接)
  • 它没有用.. 我认为可能是这样,但我仍然只有一个数据源在 WL 中运行
  • 这是因为在您的 WL 中“KironTabelaApp”数据源没有运行。您需要转到监控选项卡并验证“KironTabelaApp”和“KironLocal”是否处于运行状态..
  • 首先您需要解决您的 WL 问题,然后是应用程序代码。在您的 WL 运行 2 个必需的 DS 之前,什么都不会向前发展 ..
  • 您的 WL 中是否配置了其他 DS?如果有,你开始了吗??您能否提供-WL 的DS 屏幕截图和WL 的DS 监控选项卡屏幕截图??
【解决方案2】:

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

【讨论】:

  • 就是这样。我说对了,您的第二个 DS 可能无法运行。因此,现在单击“KironTabelaApp”数据源。转到控制选项卡,然后选择数据源,然后启动它。基本上你需要启动“KironTabelaApp”DS。完成后,再次转到数据源 -> 监控选项卡并验证两个 DS 都已启动并运行..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多