简体   繁体   English

连接池和Oracle Seesion

[英]Connection Pooling and Oracle Seesion

Before I start with my question, I would like to clarify that I am a DB developer and have limited understanding of things on Java/J2EE side. 在开始我的问题之前,我想澄清一下我是一名数据库开发人员,并且对Java / J2EE方面的知识了解有限。

Ours is a web application (n-tier with app server/web server). 我们的是一个Web应用程序(与应用程序服务器/ Web服务器处于n层)。 We are using connection pooling to manage connections to the database. 我们正在使用连接池来管理与数据库的连接。 I have limited understanding of connection pooling - App server manages connections for applications, lets the application get a connection from a pool, return the connection once its done back to the pool. 我对连接池的了解有限-App Server管理应用程序的连接,让应用程序从池中获取连接,一旦连接完成再返回到池中。

Let's say that I follow these steps - 
1. Let's say that I log in the application
2. Application requests for a connection from connection pool to authenticate me
3. Once authentication is done, App server will return the connection back to pool
4. I browse to a page where I have to do some CRUD operation and let's say that I am updating some data on the page.
5. App Server will again request for a connection from Pool
6. Application will process the data using the connection.

Here is my problem statement - 这是我的问题陈述-

Let's say that I have to capture audit information using triggers (on the tables which are undergoing update). 假设我必须使用触发器(正在更新的表上)捕获审计信息。 One of the attribute which I need to capture is username (logged in user). 我需要捕获的属性之一是用户名 (登录用户)。

I set a global package variable when I log in (step 1 - 3), which stores the logged in user name. 登录时(步骤1-3),我设置了一个全局包变量,该变量存储已登录的用户名。 My trigger is going to read the global package variable for the username . 我的触发器将读取用户名的全局包变量。 Since the connections are not going to remain same (connection pool manages connection), will my global package variable be available when I am processing the trigger? 由于连接不会保持相同(连接池管理连接),因此在处理触发器时,全局包变量是否可用?

What will happen to the variable (it obviously depends on answer to first question) when multiple users are logged in and accessing the application? 当多个用户登录并访问应用程序时,变量将发生什么(显然取决于第一个问题的答案)?

I tried to look around but have not been able to get clear answer to my doubts. 我试图四处张望,但未能获得明确的答案。

Pardon me, if my question is not clear. 如果我的问题不清楚,请原谅我。 Let me know and I can edit to provide more information. 让我知道,我可以进行编辑以提供更多信息。

You can use CLIENT_IDENTIFIER attribute to preserve the actual user who logged in to the application. 您可以使用CLIENT_IDENTIFIER属性保留登录到应用程序的实际用户。

Please find below more information from Oracle documentation: 请从Oracle文档中找到以下更多信息:

Support for Application User Models by Using Client Identifiers 使用客户端标识符支持应用程序用户模型

Many applications use session pooling to set up a number of sessions to be reused by multiple application users. 许多应用程序使用会话池来设置多个会话,以供多个应用程序用户重用。 Users authenticate themselves to a middle-tier application, which uses a single identity to log in to the database and maintains all the user connections. 用户向中间层应用程序进行身份验证,该中间层应用程序使用单个身份登录数据库并维护所有用户连接。 In this model, application users are users who are authenticated to the middle tier of an application, but who are not known to the database. 在此模型中,应用程序用户是经过应用程序中间层身份验证但数据库未知的用户。 Oracle Database supports use of a CLIENT_IDENTIFIER attribute that acts like an application user proxy for these types of applications. Oracle数据库支持使用CLIENT_IDENTIFIER属性,该属性的作用类似于这些类型的应用程序的应用程序用户代理。

In this model, the middle tier passes a client identifier to the database upon the session establishment. 在此模型中,中间层在会话建立时将客户端标识符传递给数据库。 The client identifier could actually be anything that represents a client connecting to the middle tier, for example, a cookie or an IP address. 客户端标识符实际上可以是代表连接到中间层的客户端的任何内容,例如cookie或IP地址。 The client identifier, representing the application user, is available in user session information and can also be accessed with an application context (by using the USERENV naming context). 代表应用程序用户的客户端标识符在用户会话信息中可用,并且也可以使用应用程序上下文(通过使用USERENV命名上下文)进行访问。 In this way, applications can set up and reuse sessions, while still being able to keep track of the application user in the session. 这样,应用程序可以建立和重用会话,同时仍然能够在会话中跟踪应用程序用户。 Applications can reset the client identifier and thus reuse the session for a different user, enabling high performance. 应用程序可以重置客户端标识符,从而为其他用户重用会话,从而实现高性能。

You can set the CLIENT_IDENTIFIER in java using the following code snippet: 您可以使用以下代码段在Java中设置CLIENT_IDENTIFIER:

public Connection prepare(Connection conn) throws SQLException { String prepSql = "{ call DBMS_SESSION.SET_IDENTIFIER('userName') }"; CallableStatement cs = conn.prepareCall(prepSql); cs.execute(); cs.close(); return conn; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM