Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you get a Session with <code>sessionFactory.getCurrentSession()</code>, the Session is flushed and closed automatically when the transaction is commited (see <a href="https://developer.jboss.org/wiki/Sessionsandtransactions" rel="nofollow noreferrer">Sessions and transactions</a> for more details on this). So, here I suspect that 1. you get the Session once for all (this would explain why the first call works and why subsequent calls fail) which is wrong and 2. you seem to use the <em>session-per-operation</em> anti-pattern which is even worse.</p> <p>In a web application, you should use a <em>session-per-request</em> strategy which means that "<em>a single Session and a single database transaction implement the processing of a particular request event</em>". Again, refer to the <a href="https://developer.jboss.org/wiki/Sessionsandtransactions" rel="nofollow noreferrer">Sessions and transactions</a> document. </p> <p>And if you want to remove transaction demarcation from your data access code, then you could use an <a href="https://developer.jboss.org/wiki/Sessionsandtransactions#jive_content_id_Custom_transaction_interceptors" rel="nofollow noreferrer">interceptor</a> to start a database transaction at the begin of each request and commit it at the end of the request. Have a look at the <a href="https://developer.jboss.org/wiki/OpenSessionInView" rel="nofollow noreferrer">Open Session in View</a> for an implementation of this pattern (with a sample DAO demonstrating the benefits).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload