Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java based Database Connection Pool library for multiple database users

Our Java application (web application that runs on Jetty 7.5.4) uses an underlying database. There are multiple databases users and the Java part needs to access the DB using those database users. Am wondering if there is a database connection pool library that lets us access the DB with multiple database users. I know there are a bunch of dbcp libraries that lets us use with single database user, but am unable to find any library that supports multiple database users.

Any help is appreciated, Thanks, Pram.

like image 908
prams Avatar asked Dec 18 '12 18:12

prams


People also ask

Is it possible to connect to multiple databases in Java?

Connection mysqlCon = DriverManager. getConnection(mysqlUrl, "root", "password"); To connect to multiple databases in a single JDBC program you need to connect to the two (or more) databases simultaneously using the above steps.

What is database connection pooling in Java?

Connection pooling is a technique of creating and managing a pool of connections that are ready for use by any thread that needs them. Connection pooling can greatly increase the performance of your Java application, while reducing overall resource usage.

How does JDBC connection pool work?

A JDBC connection pool is a group of reusable connections for a particular database. Because creating each new physical connection is time consuming, the server maintains a pool of available connections to increase performance. When an application requests a connection, it obtains one from the pool.


2 Answers

Tomcat DBCP definitely supports multiple users (source code). I'm not exactly sure how to configure it, but it has implemented DataSource.getConnection(username, password), which is the key feature needed.

like image 101
ngreen Avatar answered Sep 21 '22 16:09

ngreen


In addition to ngreen's comment above, you also need to set AlternateUsernameAllowed to true in your data source properties, however you still need to set a username / password on the properties as this is used when the pool is created.

/**
 * Returns true if the call {@link DataSource#getConnection(String, String) getConnection(username,password)} is
 * allowed. This is used for when the pool is used by an application accessing multiple schemas.
 * There is a performance impact turning this option on.
 * @return true if {@link DataSource#getConnection(String, String) getConnection(username,password)} is honored, false if it is ignored.
 */
public boolean isAlternateUsernameAllowed();
like image 32
Graham S Avatar answered Sep 20 '22 16:09

Graham S