I need to provide a pool of connections to a LDAP db, I'm using unbound the problem is that in the structure that we are using, I need to create one instance of the connection and this instance must manage the resource usage limits as max connecctions, similar as creating an instance of Mongoclient
The MongoClient class is designed to be thread safe and shared among
threads. Typically you create only 1 instance for a given database
cluster and use it across your application.
IMPORTANT When creating many MongoClient instances:
All resource usage limits (max connections, etc) apply per MongoClient
instance To dispose of an instance, make sure you call
MongoClient.close() to clean up resources
The Unboundid sdk has the classes LDAPConnectionPool class and the LDAPConnection. But do not have something that is similar to Mongoclient, for creating a single instance of an lDAPClient and this will be the one that take care of the usage limits. How can I implement this, this are examples for creating connection pools, but I think that this only create the connection pool but doesn't have an object as mongoclient
// Create a new LDAP connection pool with ten connections established and
// authenticated to the same server:
LDAPConnection connection = new LDAPConnection(address, port);
BindResult bindResult = connection.bind(bindDN, password);
LDAPConnectionPool connectionPool = new LDAPConnectionPool(connection, 10);
So my question is how can I build an LDAPclient similar to MOngoclient, or it is a library that I can use
Related
I have the below doubts on the Hikari or any database pooling concept.
When does the connection pool gets created?
Suppose if I have mentioned spring.datasource.hikari.maximum-pool-size=50
Will it create 50 database instances?
As by default, Spring scopes on classes in singleton, how 50 instances are created?
1:
A connection pool will be created when the first connection is creating. For example when the first SQL is executed. {#link com.zaxxer.hikari.HikariDataSource#getConnection()}.
This is my case and maybe it's different according the ORM you are using.
Connection instances will be created by the connection pool. {#link com.zaxxer.hikari.pool.HikariPool#poolEntryCreator} {#link com.zaxxer.hikari.pool.HikariPool#postFillPoolEntryCreator}
"spring.datasource.hikari.maximum-pool-size=50" means that the connection pool would not create connection instance more than 50. So it is the limit for connection instance count in connection pool.
2:
Spring Bean is singleton by default. But connection instances in the conneciton pool are not "spring bean" and they are created from "new PoolEntry". {#link com.zaxxer.hikari.pool.HikariPool#createPoolEntry}
Connection pooling helps reducing in creating database connections everytime a database call is encountered.
Pool would be nothing but set of connections (ideally active) (Like we have thread pool ), when requested will return one active connection ( or create one for first database request) , instance would be a misfit word here !
To answer first part of your question, it is initialized during application startup when that spring.datasource.hikari... property is encountered.
Below link explains this concept well
https://coderstea.in/post/best-practices/jdbc-connection-pooling-explained-with-hikaricp/
I have assigned hibernate session to instance variable of an immutable class. The class uses this session object (instance variable) to create hibernate query. The code works fine.
Due to this is it possible that the connection pool does not have free connections to database.
Session will take a single connection from the connection pool. Other connections will be free. Please refer the Hibernate manual,
*It is advisable to have the org.hibernate.SessionFactory create and pool JDBC connections for you. If you take this approach, opening a org.hibernate.
Session is as simple as:
Session session = sessions.openSession(); // open a new Session
Once you start a task that requires access to the database, a JDBC connection will be obtained from the pool.*
I have a basic question about the Tomcat 7 JDBC Connection Pool: is a separate pool created for each separate database (i.e., URL)? Or is a single pool created that holds open connections from any number of different databases?
For example, if I open connections to databases A and B by doing this:
PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName("org.postgresql.Driver");
poolProperties.setUrl("jdbc:postgresql://myserver/db_a");
poolProperties.setInitialSize(1);
poolProperties.setMaxActive(10);
poolProperties.setMaxIdle(1);
poolProperties.setMinIdle(0);
and then this:
PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName("org.postgresql.Driver");
poolProperties.setUrl("jdbc:postgresql://myserver/db_b");
poolProperties.setInitialSize(1);
poolProperties.setMaxActive(10);
poolProperties.setMaxIdle(1);
poolProperties.setMinIdle(0);
Have I just created one pool with a maxActive of 10, or two pools, each with a maxActive of 10? If it's one pool, what if I had changed maxActive to, say, 30 when opening the connection for database B? Does the first call to setMaxActive win, or does the second call override, or does this cause a separate pool to be created?
Okay, I did some digging and figured this out myself. (Thanks for the many kind folks on the tomcat-users mailing list!)
JB Nizet is right: if you are creating Tomcat database connection pools from Java code, each DataSource you instantiate literally is/represents a separate connection pool. This was surprising to me; coming from a .NET background, I assumed the Tomcat connection pooling would work like SqlServer/ADO.NET connection pooling: if you use two identical connection strings to get two database connections, these will both come from the same connection pool. However, in Tomcat, when instantiating DataSource objects from Java code, each new DataSource instance is a whole new connection pool. So, if you want to persist these connection pools across JAX-RS web service calls, for example, you need to build your own database-pool (DataSource) cache, put the DataSource instances (one per database) into it, and store it in an object that JAX-RS will persist across web service calls. I just did this, and it is working fine.
btw, Tomcat database connection pooling does offer functionality similar to SqlServer/ADO.NET connection pooling, you just have to use JNDI resources to create your DataSource instances. (In my case this is not an option, since databases are created dynamically in my application, and JNDI definitions are generally created from config files that Tomcat reads at startup.)
In a very large project where do we set up the database connection so that it is available across all the modules?
Suppose the requirement is like this:
LoginPage.html -> LoginServlet.java -> LoginService.java ==> Takes DB help to check the credentials.
Now, since the actual credentials are stored in DB, where do we set up the database so that the connection is available to all the modules?
In big projects, is database connection made as and when needed or database connections setup at the time when application is run and made available across all the modules.
If DB connections are made available to all the modules (which need DB connectivity), how is this achieved?
Thanks for your help and inputs.
Since you're not using an IoC approach (Spring), the alternative would be to have a static class (or a singleton) that has a reference to the DataSource. Whenever you need a Connection you only have to get it from that class:
public class JdbcUtils{
private static DataSource dataSource;
static{
dataSource = new DB2SimpleDataSource();
dataSource.setDatabaseName("DBNAME");
dataSource.setServerName("xxx.xxx.xxx.xxx");
dataSource.setPortNumber(447);
dataSource.setUser("USER");
dataSource.setPassword("PASS");
dataSource.setDriverType(4);
dataSource.setCurrentSchema("SCHEMA");
//OR even better get the DataSource through JNDI lookup if defined on server
}
public static Connection getConnection() throws SQLException{
return dataSource.getConnection()
}
}
What is the difference between javax.sql.DataSource and javax.sql.ConnectionPoolDataSource? I don't know which resource type to use when I am creating a connection pool in GlassFish for MySQL.
ConnectionPoolDataSource is just a DataSource (as it inherit's CommonDataSource which is also inherited by DataSource) with capability of Connection Pooling -
you asked : I don't know which resource type to use
It depend's on your application, many database drivers take a long time to create a new connection with database, If your application is going to create too many connection's (very frequently). use connection pooling.