使用连接池或会话管理数据库连接。如何?
我有一个简单的任务要完成,但我不确定最好的方法是什么。
每个用户都有自己的用户名和密码,以不同的权限连接到数据库。一旦用户连接,他将根据他想要执行的操作执行多个查询。因此我想保留与数据库的连接。这是我的问题吗?
通过连接池或会话或两者来实现我想要做的事情是否更好? 示例代码将不胜感激!非常感谢。
I have a simple task to accomplish, but I am not sure what is the best way to go by.
Each user has their own username and password to connect to a database with different privilege. Once the user connect, he will do multiple query base on what action he want to perform. Therefore I want to retain the connection with the database. So here is my question?
Is it better to achieve what I want to do with connection pooling or session or both?
Sample code would be appreciated!!! thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在服务器中定义一个具有“root”权限的容器管理连接池
DataSource
,并为每个用户使用DataSource#getConnection()
以用户名和密码作为参数的方法。Define a container managed connection pooled
DataSource
with "root" privileges in the server and for each user make use of theDataSource#getConnection()
method which takes an username and password as arguments.我在使用 Oracle DB 时也遇到了同样的问题,其中每笔交易都必须记录下来以供可能的审计。
因此,每次用户在 Web 应用程序中进行身份验证时,它都会放置一个如下所示的对象:
然后我实现了一个连接池(基于 Map),其中键是 ConnectionUser 对象,值是打开的连接。如果映射中不存在该键,则会创建一个连接并将其分配到映射中。
我希望它对你有帮助。
I faced the same issue with an Oracle DB, where every transaction has to be record for possible audits.
So, every time a user authenticates in the web application, it puts an object like this:
Then I implemented a connection pool (based on a Map) where the key is the ConnectionUser object and the value is the open connection. If the key doesn't exist in the map, then it creates a connection and allocates it into the map.
I hope it helps you.