在 JDBC 连接上切换用户

发布于 2024-08-18 01:39:49 字数 444 浏览 6 评论 0原文

我正在编写一个连接到 Oracle 11g 数据库并使用 c3p0 连接池的 Java JDBC 数据库应用程序。出于示例目的,我有 3 个数据库用户 DEFAULT、TOM 和 BILL。 c3p0 使用 DEFAULT 数据库用户打开所有池连接。我想从 c3p0 检索池连接之一,并将连接的用户更改为 BILL 而不是 DEFAULT。是否可以在 JDBC 中执行此操作而无需与数据库建立新连接?

我已经尝试过执行以下操作:

connect BILL/password;

但这不起作用。我收到一条错误消息“

java.sql.SQLException: ORA-00900: invalid SQL statement

还有其他选项吗?”是否有与上下文设置或切换有关的事情可以促进我想做的事情?

谢谢!

I am writing a Java JDBC database application that connects to an Oracle 11g database and am using a c3p0 connection pool. For the purposes of an example, I have 3 database users DEFAULT, TOM, and BILL. c3p0 opens all of the pooled Connections with the DEFAULT database user. I would like to retrieve one of the pooled Connections from c3p0 and change the user for the Connection to be BILL instead of DEFAULT. Is it possible to do this in JDBC without establishing a new connection with the database?

I have already tried doing the following:

connect BILL/password;

But this does not work. I get an error saying

java.sql.SQLException: ORA-00900: invalid SQL statement

Are there any other options? Is there something having to do with context set or switching that can facilitate what I'm trying to do?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

執念 2024-08-25 01:39:49

昨天研究了一下,发现解决办法是使用Oracle Proxy Authentication。该解决方案不符合 JDBC 规范。然而,Oracle提供了一个钩子来实现这样的解决方案。打开代理连接如下所示:

import oracle.jdbc.OracleConnection;    

//Declare variables
String url = "...";
String username = "...";
String password = "...";

//Create the Connection
Connection conn = DriverManager.getConnection(url, username, password);

//Set the proxy properties
java.util.Properties prop = new java.util.Properties();
prop.put(OracleConnection.PROXY_USER_NAME, "BILL");
prop.put(OracleConnection.PROXY_USER_PASSWORD, "password");

//Cast the Connection to an OracleConnection and create the proxy session
((OracleConnection)conn).openProxySession(OracleConnection.PROXYTYPE_USER_NAME, prop);

/* The Connection credentials have now been changed */

如果存在与此相关的其他细微差别,我不会感到惊讶,但这是一个好的开始。谢谢大家的帮助!

After researching yesterday, I found that the solution is to use Oracle Proxy Authentication. This solution is outside of the JDBC specification. However, Oracle provides a hook to implement such a solution. Opening a proxy connection would look like as follows:

import oracle.jdbc.OracleConnection;    

//Declare variables
String url = "...";
String username = "...";
String password = "...";

//Create the Connection
Connection conn = DriverManager.getConnection(url, username, password);

//Set the proxy properties
java.util.Properties prop = new java.util.Properties();
prop.put(OracleConnection.PROXY_USER_NAME, "BILL");
prop.put(OracleConnection.PROXY_USER_PASSWORD, "password");

//Cast the Connection to an OracleConnection and create the proxy session
((OracleConnection)conn).openProxySession(OracleConnection.PROXYTYPE_USER_NAME, prop);

/* The Connection credentials have now been changed */

I wouldn't be surprised if there are other nuances associated with this, but this is a good start. Thanks for your help, everyone!

静待花开 2024-08-25 01:39:49

检查

Oracle VPD 的 JDBC 扩展

设置 OracleConnection.clientIdentifier 外观更标准/适合我

很抱歉在旧线程上发帖,只是想更新。

Check

JDBC Extension for Oracle VPD

Setting OracleConnection.clientIdentifier looks more standard / suitable to me

Sorry to post on old thread, just thought of updating.

帅气尐潴 2024-08-25 01:39:49

如果这些用户不通过您的应用程序以交互方式登录数据库,那么只拥有三个独立的池(每个用户一个)是否不合理?然后使用一些连接管理器来检索适当的连接?

If these users do not login interactively to the database via your application, is it unreasonable to just have three separate pools, one for each user? Then use some connection manager to retrieve the appropriate connection?

故人如初 2024-08-25 01:39:49

您可以使用DataSource.getConnection(String user, String password)。 c3p0 在内部为每个用户维护一个单独的池。

You can use DataSource.getConnection(String user, String password). c3p0 internally maintains a separate pool for each user.

埖埖迣鎅 2024-08-25 01:39:49

c3p0 使用您告诉他使用的凭据创建物理连接,并且事后您无法更改从池中获取的连接的凭据。如果要与不同的数据库用户使用连接,则需要创建和使用不同的池。

c3p0 creates physical connections with the credential you told him to use and you can't change the credentials of a connection obtained from a pool after the facts. If you want to use connections with different database users, you need to create and use different pools.

南七夏 2024-08-25 01:39:49

您是否尝试过通过 jbdc 发出此语句:

alter session set current_schema=BILL.

如果我没记错 Oracle 结构,则您连接的用户名与您正在处理的模式相同。

我过去确实通过 jdbc 在 Oracle 10 中成功使用了上述语句。我的用户是 root/admin 用户,它拥有各种数据库模式的权限,我需要在同一连接中在它们之间进行切换。请注意,我不需要再次提供密码。

这听起来不像是一个非常注重安全的模型,所以我不知道它是否适合您的用例。

Have you tried issuing this statement via jbdc:

alter session set current_schema=BILL.

If I remember correctly the oracle structure, the username with which you connect is the same as the schema you are working on.

I did use the above statement successfully in the past with Oracle 10 via jdbc. My user was the root/admin user, it had permissions to various database schemas and I had a need to switch between them in the same connection. Notice that I didn't need to supply a password again.

This doesn't sound like a very security-conscious model, so I don't know if it is suitable for your use-case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文