0

So,

The hibernate SessionFactory is available.

Now, on java.sql.Connection there is a method connection.setTransactionIsolation.

Now, from sessionFactory you can openSession(), or getCurrentSession()

From there, we can do:

session.doWork(connection -> { ... connection.setTransactionIsolation ... } )

However, my memory tells me this is already too late.

One actually needs to do connection.setTransactionIsolation likely before the Session has even been created.

Is this not true?

Further, setReadOnly has the same requirements.

Now, to get the connection before the session is created there is this answer:

https://stackoverflow.com/a/29844998/961018

But that requires the datasource.

So is there anyway one, can get the datasource from SessionFactory, so I can create this logic from what I currently have?

EDIT

RESPECTFULLY, DO NOT UTTER THE WORDS S.P.R.I.N.G or A.N.N.O.T.A.T.I.O.N.S.

mjs
  • 21,431
  • 31
  • 118
  • 200

1 Answers1

2

The "Transaction Isolation" property has got a "Connection" level, that's why you have to apply it once creating the connection and not at transaction level or (like general rule for all connection) on SessionFactory.

Considering what you are trying to do, you have actually two different options to set the transaction isolation.

The most used, clean and recommended one is by setting the specific hibernate property, like following one:

<property name="hibernate.connection.isolation">2</property>

On the specific case, value 2 correspond "READ COMMITTED".

Otherwise, you could try to get the connection instance from the Session, like following code:

Session session = sessionFactory.getSession();

try {
    session.connection().setTransactionIsolation(2);
} catch (HibernateException | SQLException e) {
    e.printStackTrace();
}

Please bear in mind that the above is quite a dirty way to do that and it's not guarantee that it works. Moreover the method connection() of org.hibernate.Session is deprecated, so you should not use it.

However, I personally used for some unit tests (as an experiment) and it worked fine.

Marco Tizzano
  • 1,726
  • 1
  • 11
  • 18
  • Yes, the connection() method is no longer there, but since it worked for you, doWork should work fine here. Otherwise i could possibly cast to SessionImpl I guess the goal is to do it prior to actually creating the transaction. I am aware of the global setting, and I am just trying to enable it per transaction / connection rather than a global setting. – mjs Mar 12 '21 at 14:30
  • In the link in the description of the question, they are actually setting these properties before even creating the session. I am unsure if it is generally too late then. – mjs Mar 12 '21 at 14:31
  • yes indeed. Actually, I could do in that way because in my company we were still using hibernate core 3.3.1GA, and the method was there, although deprecated. Let's say I was still on time and lucky enough to do it programmatically :) – Marco Tizzano Mar 12 '21 at 14:34