How to get new connection object in hibernate - java

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?

Related

When does a Hikari pool connections work? - Spring/Spring Boot

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/

Can hibernate session be assisgned to a instance variable of a Class

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.*

Java DataSource SessionFactory

I'm confused about the difference between DataSource and SessionFactory.
I think SessionFactory is a manager for retrieve Sessions (which are in fact connections to a database I guess).
DataSource has the method getConnection()
"Attempts to establish a connection with the data source that this DataSource object represents."
It means a DataSource object always works directly with the database?
If I have a pool of connections, DataSource will ask to the pool for a connection? Or will it try to get another connection to the database.
Are in fact DataSource and SessionFactory the same? Both try to give sessions/connections?
Datasource maintains the pool of connections, establishes the connection, knows how to connect etc.,
User configures data source either in the server(like websphere/weblogic) or in the spring/hibernate configuration file. Either way you give the handle of this data source to the sessionfactory during the app startup.
SessionFActory is an interface which hides all the details like opening the connection / closing the connections. Through out your development time you dont worry about these small things like loading driver/closing connection/opening etc., so you can concentrate on business logic/other important stuff.

tomcat 7 JDBC connection pool - separate pool for each database?

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.)

Difference between DataSource and ConnectionPoolDataSource

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.

Categories

Resources