我的 WebLogic Server 上有两个DataSource,每个都访问不同的数据库。
在我的客户端应用程序中,我有一些方法需要连接到第一个 DB和其他需要连接到 second 的人。
但是当我运行它时,它只是连接我执行的第一个方法。
例如,如果我执行一个获取第一个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;
}
答案 0 :(得分:1)
在这两种情况下,您都使用private static DataSource KironMySql = null;
实例。为不同的DS提供单独的DataSource
对象。
在尝试获取KironLocal
DS连接时,您基本上屏蔽了KironTabelaApp
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;
}
}