9

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?

1 Answer 1

12

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();
3
  • @Pau Kiat Wee Thank you, your answer helped me add connection pool into existing app without much effort Commented Aug 18, 2016 at 21:22
  • 1
    It may be a version thing, but withOptions() to me is shown as a non-static method. In this case, you need to create an instance of SessionFactory. Commented Aug 29, 2017 at 19:28
  • 1
    @PauloMerson - agreed. This example (especially since it is the accepted answer) should be updated to make it clear that withOptions() is NOT a static method.
    – Greg Brown
    Commented Sep 10, 2023 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.