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/
Related
Hibernate use sessions to interact with the database, then what is the use of connection pool?
As per the configuration, hibernate default connection pool size is 1. If we open multiple sessions(as per the default configuration each session is a thread - current_session_context_class) in the hibernate, all those sessions will use the same connection object? If not what is the use of taking new connection in hibernate and how to get that new connection object?
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.)
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.
How properly "lifecycle" of a Hibernate session under Spring should be done?
The SessionFactory is created automatically by Spring and is taking its DB connections from Glassfish connection pool. At the moment I am getting a Hibernate session via SessionFactory.getCurrentSession(). Then I start transaction, do the work and then commit() or rollback() at the end. Do I need to do any other actions like disconnect(), close(), flush() or any others at any time so connections would be properly returned back to the pool or is everything already automatically done by Spring?
With plenty of these methods it is a little confusing for me at the moment to understand when what should be done, maybe someone can point to right direction?
As SessionFactory is created automatically by Spring, Spring framework will take care of closing the connection.
Check out Spring Resource Management
If you want to check. You can check the log, if you are using logging for your app. It'll be like :
(main) INFO [AnnotationSessionFactoryBean] Closing Hibernate SessionFactory
I get following lines from this link
The main contract here is the creation of Session instances. Usually
an application has a single SessionFactory instance and threads
servicing client requests obtain Session instances from this factory.
The internal state of a SessionFactory is immutable. Once it is
created this internal state is set. This internal state includes all
of the metadata about Object/Relational Mapping.
Implementors must be threadsafe.
The policies about how the connection releases back to the connection pool have nothing to do with Spring .It is configured by Hibernate itself through the configuration parameter hibernate.connection.release_mode , which is identified by the enum in the org.hibernate.ConnectionReleaseMode
Start from version 3.1+ , the default value of the hibernate.connection.release_mode is auto which the corresponding ConnectionReleaseMode value depends on whether JTA or JDBC transaction is used. In case of JDBC transaction is used , it is set to ConnectionReleaseMode.AFTER_TRANSACTION (i.e after_transaction ).
The behaviour of ConnectionReleaseMode.AFTER_TRANSACTION is that : The connection will be returned to the connection pool after each transaction , that is by calling either transaction.commit() or transaction.rollback() , as well as calling session.close() and session.disconnect()
You can verify this behaviour in hibernate documentation Section 11.5.
Hope this link will guide you about session and transactions.
Then I start transaction, do the work and then commit() or rollback()
at the end. Do I need to do any other actions like disconnect(),
close(), flush() or any others at any time so connections would be
properly returned back to the pool or is everything already
automatically done by Spring?
As you call commit() on Transaction it will automatically closes the session, which ultimately calls close method on connection to return to it's pool.
When you are executing a hibernate query through SessionFactory.getCurrentSession() , Spring performs the necessary task of opening and closing the connection . The SessionFactory you are using in the spring config also calls the config.buildSessionFactory method internally .
Most of this happens in the implementations of the AbstractSessionFactoryBean. The closing of connecting is done by hibernate in the SessionFactoryImpl class using the statement settings.getConnectionProvider().close(); . In short , hibernate does everything for you . Spring just calls it's help when necessary.