访问 WebSphere 数据源,无需在独立的 Java 应用程序中指定用户名/密码。

huangapple go评论75阅读模式
英文:

Access Websphere datasource from standalone java application without specifying username/password

问题

以下是翻译好的代码部分:

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class TestDS {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Hashtable<String, String> pdEnv = new Hashtable<String, String>();
        pdEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2809");
        System.out.println("获取连接");
        Context initialContext;
        try {
            System.setProperty("com.ibm.CORBA.Debug", "true");
            System.setProperty("com.ibm.CORBA.CommTrace", "true");
            System.setProperty("com.ibm.CORBA.Debug.Output", "/usr/app/corba.log");
            initialContext = new InitialContext(pdEnv);
            System.out.println("获取初始上下文");
            DataSource datasource = (DataSource) initialContext.lookup("jdbc/sampleDB");
            System.out.println("获取数据源");
            Connection connection = null;
            System.out.println("数据源是 " + datasource);
            if (datasource != null) {
                // connection = datasource.getConnection("username","password"); // 数据库凭据
                connection = datasource.getConnection();
            } else {
                System.out.println("查找数据源失败。");
            }
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

这部分代码出现了以下错误:

java.sql.SQLNonTransientException: [jcc][t4][10205][11234][4.21.29] 不支持空用户标识。ERRORCODE=-4461,SQLSTATE=42815 DSRA0010E: SQL 状态 = 42815,错误码 = -4,461

但是如果提供用户名/密码,它可以正常工作:

connection = datasource.getConnection("username", "password");

如何在获取数据库连接时不指定用户名/密码使其正常工作呢?

英文:

I am writing a standalone application which will get the datasource from Websphere Application Server, get the db connection and perform some business logic.

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class TestDS {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable&lt;String, String&gt; pdEnv = new Hashtable&lt;String, String&gt;();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,&quot;com.ibm.websphere.naming.WsnInitialContextFactory&quot;);             
pdEnv.put(Context.PROVIDER_URL, &quot;iiop://localhost:2809&quot;);
System.out.println(&quot;Getting connection&quot;);
Context initialContext;
try {
System.setProperty(&quot;com.ibm.CORBA.Debug&quot;,&quot;true&quot;);
System.setProperty(&quot;com.ibm.CORBA.CommTrace&quot;,&quot;true&quot;);
System.setProperty(&quot;com.ibm.CORBA.Debug.Output&quot;,&quot;/usr/app/corba.log&quot;);
initialContext = new InitialContext(pdEnv);
System.out.println(&quot;Getting initial context&quot;);
DataSource datasource = (DataSource)initialContext.lookup(&quot;jdbc/sampleDB&quot;);
System.out.println(&quot;Getting datasource&quot;);
Connection connection = null;
System.out.println(&quot;Datasoure is &quot;+ datasource);
if (datasource != null) {
//connection = datasource.getConnection(&quot;username&quot;,&quot;password&quot;); // DB credintials
connection = datasource.getConnection();
} else {
System.out.println(&quot;Failed to lookup datasource.&quot;);
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

This gives the following error

java.sql.SQLNonTransientException: [jcc][t4][10205][11234][4.21.29] Null userid is not supported. ERRORCODE=-4461, SQLSTATE=42815 DSRA0010E: SQL State = 42815, Error Code = -4,461

But if provide userid/password, It works fine

 connection = datasource.getConnection(&quot;username&quot;,&quot;password&quot;);

How do I get it working without specifying userid/password when getting db connections

答案1

得分: 1

大多数JDBC驱动程序供应商允许在其数据源上设置用户名和密码,这将成为所有未提供用户/密码的请求连接的默认值。如果您进入WebSphere应用服务器中的数据源配置,然后进入“自定义属性”,您应该能够添加“用户”和“密码”的属性,如果JDBC供应商支持,这些属性将应用于数据源。

英文:

Most JDBC driver vendors allow a user and password to be set on their data sources which then serves as a default for all connections that are requested without a user/password. If you go to the configuration for your data source in WebSphere Application Server, and then to "Custom properties", you should be able to add properties for "user" and "password", which should then be applied to the data source if the JDBC vendor supports it.

huangapple
  • 本文由 发表于 2020年10月18日 03:18:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64406407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定