Alex Rivera | Logout

Hibernate + Swing

Asked 2009-05-18T01:09:30.910
12

What's the best practice?

  • A single session for the whole app
  • A session per window
  • A session per thread, and detaching everything at random
  • Something else?

I've googled around, and there's no consensus. Everyone says something, and then takes it back. I find lugging a session around stupid...

So, what's the deal?

Edit
Report

2 Answers

1

It really depends on the app, but really you only want a session to last as long as the logical transaction on the database. I'm pretty sure sessions aren't thread-safe so you may want to look at localising the behaviour to a single class.

answered 2009-05-18T01:13:20.230
0

What you really want to avoid is the session-per-operation scenario, where for every single database operation a new Hibernate session is created and closed.

EDIT: Ignore my earlier nonsense about one session for the lifetime of the app, it's entirely possible (and probably the most efficient way) of doing this. Just make sure to demarcate your transactions appropriately - be it declaratively using something like Spring @Transaction, or programmatically.

answered 2009-05-25T11:00:27.440

Your Answer