-->
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.  [ 12 posts ] 
Author Message
 Post subject: when do I explicitly close a session.
PostPosted: Fri Sep 29, 2006 6:18 am 
Newbie

Joined: Thu Sep 28, 2006 11:46 pm
Posts: 6
Hi, Ive read this:

"Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html"

but I still dont u\quite understand it. When do I explicitly close a session?In my web app, the user logs on, then performs a bunch of operations which most likely involve database interaction over a half hour period, and doesnt really log off. they probably just close the browser or surf somewhere else.

Is it OK to open a session using hiberutil code, and never close it? or should I be explicitly calling close session after every group of database interactions?

cheers


Top
 Profile  
 
 Post subject: Re: when do I explicitly close a session.
PostPosted: Fri Sep 29, 2006 8:33 am 
Newbie

Joined: Thu Jan 26, 2006 6:19 am
Posts: 19
stefoid wrote:
Hi, Ive read this:

"Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html"

but I still dont u\quite understand it. When do I explicitly close a session?In my web app, the user logs on, then performs a bunch of operations which most likely involve database interaction over a half hour period, and doesnt really log off. they probably just close the browser or surf somewhere else.

Is it OK to open a session using hiberutil code, and never close it? or should I be explicitly calling close session after every group of database interactions?

cheers


You should close your session after groups of DB transactions, actually Hibernate 3.1 automaticly closes sessions when you call tx.commit();

However, there are some exceptions, for example BLOB fields are lazily initialized, that is to say you should keep your session opened until you call the getBLOBField method of your POJO object, otherwise an exception will be thrown.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 8:45 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
actually Hibernate 3.1 automaticly closes sessions when you call tx.commit();


No, it does not. I have no idea where you found this.

org.hibernate.Transaction.commit() flushes and closes the Session if you use the getCurrentSession() feature on a SessionFactory. It does nothing if you did get the Session with openSession().

Read it again: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 5:45 am 
Newbie

Joined: Thu Sep 28, 2006 11:46 pm
Posts: 6
so if I use the hiberutil code rovide, then I dont have to explicitly call close on a session. commit will do it for me?
ok, thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 8:48 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
so if I use the hiberutil code rovide, then I dont have to explicitly call close on a session. commit will do it for me?


If the session is closed or not automatically at transaction commit has _nothing_ to do with HibernateUtil. It depends on how you got the session, with sf.openSession() or with sf.getCurrentSession(). The job of HibernateUtil is to give you the sf.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 30, 2006 6:04 pm 
Newbie

Joined: Thu Sep 28, 2006 11:46 pm
Posts: 6
ah, ok, understood.

Christian, can you also help me with this problem?

I have a class called key which has a reference to two other classes user and keytag, which I have implemented by many-to-one, like so...

[b] <many-to-one name="keytags" column="keytag_id" class="com.server.Tag"/>
<many-to-one name="userId" column="u_id" class="com.server.User"/>[/b]


yet when I exectue the code below, I get this exception [i][WARN] IllegalArgumentException in class: com.server.Tag, getter method of property: id[/i]

As far as I can see, there is nothing wrong with my setup. the cfg files and the database scheme all match. I can read and write a Tag with no problem. I only see this exception when I try to save a key.

user = (User) result.get(0);
k = new Key();
tag = new Tag();
tag.setTags(tags);
ses.save(tag);
k.setKeyname(keyName);
k.setUserId(user.getId());
k.setKeytags(tag.getId());
[b] ses.save(k);[/b]

Any clues appreciated, thanks a lot,

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 01, 2006 8:56 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Quote:
k.setKeytags(tag.getId());


This should be:

Code:
k.setKeytags(tag);


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 01, 2006 8:46 pm 
Newbie

Joined: Thu Sep 28, 2006 11:46 pm
Posts: 6
[quote="jaikiran"][quote] k.setKeytags(tag.getId()); [/quote]

This should be:

[code] k.setKeytags(tag); [/code][/quote]

oh! so Ive been under-utilizing what hibernate does behind the scenes?

so, just to be clear, in my database schema, I have a table 'key' which has a foreign key that points to a keytag table id, but in java-land, it will be translated to a POJO reference. (currently in my java class, I have a reference to a keytag id of type Long.)

and to be doubly clear, if I load a key object, then hibernate will fetch and load the referenced keytag as well? if so, Im starting to udnerstand why this is a good thing.

sorry, this is probably dead-basic stuff. Ive had virtually no experience with databases before, and this is my first hibernate project.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 3:44 am 
Newbie

Joined: Thu Sep 28, 2006 11:46 pm
Posts: 6
thanks jaikiran,

I still got problems. unending problems due to my unending ignorance.

OK, I did as you suggested and I got my first problem. 'key' is an SQL reserved word. some random surfing of forums found that problem.

now I have a different error:

SQL Error: 1054, SQLState: 42S22
[WARN] Unknown column 'keytag_id' in 'field list'

Im not sure about this one.

here is that mapping in Key again:

<many-to-one name="keytags" column="keytag_id" class="com.server.Tag"/>
<many-to-one name="userId" column="u_id" class="com.server.User"/>

I am thinking that I dont understand what the 'column' parameter is. I think there must be a problem in my SQL table, but I cant work out what it is. Using MySQL browser, I have created a table called mykey (used to be called key, but that was a reserved word). That table has:

two colums,
1) primary key called 'id'
2) string called 'name'

and two foreign keys called keytag_id and user_id respectively. I think this is where the problem is, but what could it be.

any help appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 3:50 am 
Newbie

Joined: Thu Sep 28, 2006 11:46 pm
Posts: 6
more to this problem. I think I dont understand the difference between these two parameters of a foreign key

its 'keyname' and its 'column'

does a foreign key have to have a regular 'column' inthe table. If so, what is the the foreign key's 'keyname' if not the name of the column of the table it is in?

confusion...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 1:22 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
Going by what you describe about your tables, here's how the many-to-one should look like:

Code:
<many-to-one name="keytags"  class="com.server.Tag">
<column name="keytag_id"/>
</many-to-one>

<many-to-one name="userId" class="com.server.User">
<column name="u_id"/>
</many-to-one>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 04, 2006 3:45 am 
Beginner
Beginner

Joined: Mon Oct 11, 2004 2:22 am
Posts: 41
back to the session closing question:

In a j2ee environment many people employ one of two methods of explicitly closing sessions.

1. Request/Response context: a hibernate session is stored in the context of the processing thread (via a ThreadLocal) and at the end of every Request/Response the hibernate session is explicitly closed (if not null and open). This is best implemented as a javax.servlet.Filter.

2. Session context: a hibernate session is stored on the http session and is explicitly closed when the session is terminated/invalidated. This can be implemented via a wrapper and a javax.servlet.http.HttpSessionBindingListener.

Personally I use 1, but 2 can have some performance benefits.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.