Change user when get connection from pool

17 views
Skip to first unread message

Damien Grenier

unread,
Jan 28, 2021, 12:19:24 PM1/28/21
to mybatis-user
Hi,

I create a connection pool with a generic user.
In my web application, I want ti get a connection from the pool with the connected user.

it is possible with myBatis?

Regards,
Damien

Tim Chen

unread,
Jan 28, 2021, 12:31:12 PM1/28/21
to mybati...@googlegroups.com
Can you explain your use case more? A connection pool is created with connections based on the credentials you provide it initially.
Different users that need specific authenticated connections normally have their own pool or create their own connection directly.

On Jan 28, 2021, at 12:19, Damien Grenier <damien....@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/eb30b300-c834-4331-b462-6108975ccdcan%40googlegroups.com.

Damien Grenier

unread,
Feb 10, 2021, 9:05:56 AM2/10/21
to mybatis-user
I use the jt400 library to connect my application the DB2 for i database (IBM i).
jt400 has AS400JDBCConnectionPoolDataSource class with 2 methods : 
getPooledConnection() : returns a connection with the user who create the pool
getPooledConnection(java.lang.String user, java.lang.String password) : returns a connection with this user and password

I want to use this getPooledConnection(user,pwd) for know which is connected to the database.

Is it possible with myBatis to do this ?

Guy Rouillier

unread,
Feb 11, 2021, 2:36:43 AM2/11/21
to MyBatis User
See PooledDataSource.getConnection(String username, String password).

--
Guy Rouillier

Larry Meadors

unread,
Feb 11, 2021, 8:43:30 AM2/11/21
to mybati...@googlegroups.com
I've done this before with a web app and don't recommend it, but you can make your own data source that wraps the jt400 version then use a thread local user to provide that.

The workflow is generally:
- inspect incoming request and set user in thread local storage
- process request
- clear user from thread local storage after request is processed

In your data source, you replace the getConnection() method with one that looks in the thread local storage for the user object, then calls getConnection(user, pass) and returns that connection. If you use a proxy for the wrapped connection, you can replace just that one method.

All that said...unless you have a VERY compelling reason to do this, I do NOT recommend it. It's messy and uses up a lot of resources. If you have a legacy green-screen system that is built on the actual system user, I get that it's convenient, but investigate other options.

Larry


Reply all
Reply to author
Forward
0 new messages