调用DataSource.getConnection不返回预期的连接 [英] Call to DataSource.getConnection not returning the expected connection

查看:145
本文介绍了调用DataSource.getConnection不返回预期的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

        Hashtable env1 = new Hashtable();

        env1.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
        log.info("Executed step 1");
        env1.put(javax.naming.Context.PROVIDER_URL, "iiop://myhost.com:9301");
        log.info("Executed step 2");    

        Context ctx = new InitialContext(env1);

        DataSource ds = (DataSource)ctx.lookup("jdbc/mydatasource");
        log.info("Excecuted lookup  ="+ds);

        conn = ds.getConnection();

我在连接到WAS 6.1.0.3的独立应用程序中有以前的代码以便检索来自数据源的连接。代码非常简单,我看到相同的代码在不同的环境中工作,但在这种情况下,当我调用getConnection时,我得到一个异常。数据源是WAS具有正确的身份验证别名集,并且当连接被测试时,它可以从WAS端正常工作,但之前的代码将无法工作。

I have the previous code in an standalone application that is connecting to WAS 6.1.0.3 in order to retrieve a connection from the datasource. The code is very straighforward, and I have seen the same code working in a different environment, but in this case when I call getConnection I get an exception. The datasource is WAS has the proper authentication alias set and when the connection is tested it works OK from the WAS side, but the previous code won't work.

如果我更改此行: conn = ds.getConnection();

到此: conn = ds.getConnection(username, 密码);

然后代码就可以了!但这不是我想要的,因为数据源中的连接应该已经设置了凭据。我最初认为这是一个Sybase问题,但它也发生在Oracle上,所以宁愿说我有WAS问题。

Then the code will work! But that's not what I want since the connections in the datasource should already have the credentials set. I was initially thinking this was a Sybase problem, but it's also happening with Oracle, so would rather say I have a problem with WAS.

如果你对异常感到好奇,对于Sybase,我得到:

If you are curious about the exceptions, for Sybase I get:

java.sql.SQLException: JZ004: User name property missing in DriverManager.getConnection(..., Properties).DSRA0010E: SQL State = JZ004, Error Code = 0
    at com.sybase.jdbc2.jdbc.ErrorMessage.raiseError(ErrorMessage.java:569)
    at com.sybase.jdbc2.tds.LoginToken.<init>(LoginToken.java:128)
    at com.sybase.jdbc2.tds.Tds.doLogin(Tds.java:506)
    at com.sybase.jdbc2.tds.Tds.login(Tds.java:449)
    at com.sybase.jdbc2.jdbc.SybConnection.tryLogin(SybConnection.java:254)
    at com.sybase.jdbc2.jdbc.SybConnection.regularConnect(SybConnection.java:230)
    at com.sybase.jdbc2.jdbc.SybConnection.<init>(SybConnection.java:200)
    at com.sybase.jdbc2.jdbc.SybPooledConnection.<init>(SybPooledConnection.java:72)
    at com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource.createConnection(SybConnectionPoolDataSource.java:138)
    at com.sybase.jdbc2.jdbc.SybDriver.connect(SybDriver.java:485)
    at com.sybase.jdbc2.jdbc.SybDriver.connect(SybDriver.java:517)
    at com.sybase.jdbc2.jdbc.SybDataSource.getConnection(SybDataSource.java:227)
    at com.sybase.jdbc2.jdbc.SybConnectionPoolDataSource.getPooledConnection(SybConnectionPoolDataSource.java:74)
    at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper$1.run(InternalGenericDataStoreHelper.java:897)
    at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
    at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper.getPooledConnection(InternalGenericDataStoreHelper.java:892)
    at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getPooledConnection(WSRdbDataSource.java:1181)
    at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1047)
    at com.ibm.ws.rsadapter.spi.WSDefaultConnectionManagerImpl.allocateConnection(WSDefaultConnectionManagerImpl.java:81)
    at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:431)
    at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:400)

对于Oracle我得到这个:

And for Oracle I get this one:

java.sql.SQLException: invalid arguments in callDSRA0010E: SQL State = null, Error Code = 17,433
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:236)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:420)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
    at oracle.jdbc.pool.OracleDataSource.getPhysicalConnection(OracleDataSource.java:297)
    at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:221)
    at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPhysicalConnection(OracleConnectionPoolDataSource.java:157)
    at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPooledConnection(OracleConnectionPoolDataSource.java:94)
    at oracle.jdbc.pool.OracleConnectionPoolDataSource.getPooledConnection(OracleConnectionPoolDataSource.java:75)
    at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper$1.run(InternalGenericDataStoreHelper.java:897)
    at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
    at com.ibm.ws.rsadapter.spi.InternalGenericDataStoreHelper.getPooledConnection(InternalGenericDataStoreHelper.java:892)
    at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getPooledConnection(WSRdbDataSource.java:1181)
    at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1047)
    at com.ibm.ws.rsadapter.spi.WSDefaultConnectionManagerImpl.allocateConnection(WSDefaultConnectionManagerImpl.java:81)
    at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:431)
    at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:400)

在这两种情况下,如果我将凭证传递给getConnection方法,我将不会例外

In both cases I won't the exception if I pass the credentials to the getConnection method

感谢您的建议。

推荐答案

简短回答:外部客户无法使用认证别名数据

Short answer: external clients don't get to use the authentication alias data

更长答案:

来自 WAS J2C连接工厂文档

Longer Answer:
From the WAS J2C connection factory documentation:


为组件管理的身份验证配置的别名不适用于必须访问受保护资源的所有客户端。具有Java命名和目录接口(JNDI)访问权限的外部Java客户端可以查找Java 2连接器(J2C)资源,例如数据源或Java消息服务(JMS)队列。但是,不允许它们利用资源上定义的组件管理的身份验证别名。此别名是getConnection()方法未指定任何身份验证数据(如用户和密码)或ConnectionSpec值时使用的默认值。如果外部客户端需要获得连接,则必须通过在getConnection()调用上传递参数来承担身份验证的责任。
The alias that you configure for component-managed authentication does not apply to all clients that must access the secured resource. External Java clients with Java Naming and Directory Interface (JNDI) access can look up a Java 2 Connector (J2C) resource such as a data source or Java Message Service (JMS) queue. However, they are not permitted to take advantage of the component-managed authentication alias defined on the resource. This alias is the default value that is used when the getConnection() method does not specify any authentication data, like user and password, or a value for ConnectionSpec. If an external client needs to get a connection, it must assume responsibility for the authentication by passing it through arguments on the getConnection() call.

这篇关于调用DataSource.getConnection不返回预期的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆