I am pumping existing JDBC connection using SessionFactory.openSession(Connection)
. Now in 4.0 this method is not available. Which method should I use for this?
Most importantly, the SessionFactory in Hibernate is responsible for the creation of Session objects. The Hibernate Session provides methods such as save, delete and update, all of which are used to perform CRUD-based operations on the database to which the SessionFactory connects.
Session factory is long live multithreaded object. Usually one session factory should be created for one database. When you have multiple databases in your application you should create multiple SessionFactory object. Configuration cfg=new Configuration();
openSession() always opens a new session that you have to close once you are done with the operations. SessionFactory. getCurrentSession() returns a session bound to a context - you don't need to close this.
@DarkHorse SessionFactory is not Singleton , it is used as singleton , it is immutable docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/…
You can use SessionFactory.withOptions() and SessionBuilder.connection(Connection connection).
SessionBuilder connection(Connection connection)
Adds a specific connection to the session options
Parameters: connection - The connection to use.
Returns: this, for method chaining
Example:
SessionBuilder sb = SessionFactory.withOptions();
Session session = sb.connection(connection).openSession();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With