Ferran,

You can just @Inject the session in if you want to write less code:

class MyDAO {
  @Inject private Session session;
  public void performSomeOperationOnSession() {
    ...
  }
}

and then yes, you must use PerThreadManager explicitly if you are creating your own threads using the Dao.

Either using PerThreadManager.run(runnable) or by calling perThreadmanager.cleanup() in a finally block inside your thread.

p.

On 28/02/2012 8:37 PM, Ferran Maylinch wrote:
About creating threads explicitly... do we need to use PerthreadManager?

I saw you recommend it here:
http://tapestry.1045711.n5.nabble.com/Reusing-tapestry-hibernate-session-in-a-thread-td5438833.html

On Tue, Feb 28, 2012 at 10:30 AM, Ferran Maylinch
<ferranmayli...@gmail.com>wrote:

Thank you very much for your answers.

So, if I understood you well, I can let Tapestry inject the Session in my
DAOs this way:

class MyDAO {

   private final Session session;

   public MyDAO (Session session) {
     this.session = session;
   }

   public void performSomeOperationOnSession() {
     ...
   }
}

...and I can rest assured multiple concurrent requests that call
performSomeOperationOnSession() will use different sessions because the *
session* injected in the DAO is a per-thread-proxy. Is this right?

But, as Kalle says, I suppose we should use HibernateSessionSource when we
explicitly create multiple threads. Or even in this case injecting Session
is enough? (Because maybe it doesn't matter if threads are created
explicitly by me or implicitly by the web container).

Ferran



On Tue, Feb 28, 2012 at 2:09 AM, Thiago H. de Paula Figueiredo<
thiag...@gmail.com>  wrote:

On Mon, 27 Feb 2012 20:55:28 -0300, Ferran Maylinch<
ferranmayli...@gmail.com>  wrote:

  Hello,
Hi!


  We are currently using both approaches but we don't know exactly when to
use each one.
When should we inject a HibernateSessionSource into the DAO and create
Sessions with source.create() and when should we directly inject a
Session?

Unless in some very specific scenarios, I'd just inject a Session. In
Tapestry-Hibernate, Session is a perthread service: in other words, in
different threads, different session instances are provided. In the same
thread, all Session injections get the same instance.


  In our application we have some DAOs calling other DAOs. What should I
inject to them: a HibernateSessionSource or a Session? Or should I pass a
Session to the DAO methods?

Same answer as above: just inject Session.


  Or DAOs calling other DAOs is discouraged?
If you're working with two tiers (web and DAO), I'd say no, it isn't
discouraged. If you're working with three tiers (web, business rules and
DAO), I'd say it is discouraged and you'd better have a business rule class
call another one instead.


  And a further question would be: what happens if a class starts several
threads using Sessions and transactions. How should we handle that?
Sometimes we suffer deadlocks but we can't spot the cause.

Now you're going out of topic here. IMHO, this is a pure database and
connection question, and this is the Tapestry web and IoC framework mailing
list. :)

--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to