-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: DAO class in Caveatemptor example
PostPosted: Tue Mar 08, 2005 5:18 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 5:18 pm
Posts: 44
In the Caveatemptor example, it creates a DAO object (e.g. UserDAO, ItemDAO) every time a command is executed (e.g. BidForItemCommand)

see below:
Code:
public void execute() throws CommandException {

      try {
         ItemDAO itemDAO = new ItemDAO();
         UserDAO userDAO = new UserDAO();


Why is this necessary? Can we have a static object for each DAO? so we don' t need to create every time?

And in the constructor of UserDAO(), it begins a transaction.
when does it end?

public UserDAO() {
HibernateUtil.beginTransaction();
}

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 6:25 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why not create a new one every time? It's very lightweight. Read chapter 8 again and you will understand the design, incl. the transaction demarcation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 6:30 am 
Newbie

Joined: Wed Mar 09, 2005 4:25 am
Posts: 9
Location: Hamburg, Germany
i implemented a singleton for the dao. (ok, it is not neccessary)
or you can provide a method/constructor with an attribute (e.g.
public ItemDAO(boolean beginTransaction) {..}
to determine if a transaction should be started or if it has been already created previously


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 7:35 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Thats the job of the threadlocal binding... read again :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 7:45 am 
Newbie

Joined: Wed Mar 09, 2005 4:25 am
Posts: 9
Location: Hamburg, Germany
OK,
my post was only half of the story. When using only one transaction for the whole thread, the DAO do not need to be a singleton or something like that. Session, transaction etc. is stored in a ThreadLocal attribute.

I implemented the usage of two or more datasources and a feature to start a new user transaction. This requires XA-Transactions (Where i still having trouble when using this within a webcontainer).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.