-->
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.  [ 4 posts ] 
Author Message
 Post subject: session per application transaction
PostPosted: Wed Apr 14, 2004 10:25 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:49 am
Posts: 21
Hi i have a question regarding the session-per-application-transaction part of this link.

http://www.hibernate.org/168.html

It claims that

Quote:
Atomicity of the application transaction is only guaranteed if only one Request/Response cylce writes data, all others before just read
.

I hoped someone could explain it in more detail because I would like to use this design on my system.
I want to open of session for a registration process that span over many requests.
For each request I open a transaction, do some saving and reading from database, commit a transaction,disconnect the session. And when the whole registration process is completed I would like to close the session.

I would like to hold the session for the whole duration of the registration process, because of lazy loading. Is this a bad idea should I instead close the session, and manually lock the object to the new session?


any comments on my approach?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 7:02 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can use both styles, a Long Session or Detached Objects/Reattachment for each request/response. Keep in mind that you have to close the Long Session at some point, usually at the end of your application transaction.

The atomicity aspect is simple: If you have n database transactions in 1 application transaction, only the last database transaction should write data.

For example, if your registration process has 3 screens and you have the sequence: Screen1(load data), Screen2(load more data), Screen3(save data), your application transaction will be atomar, as Hibernate checks the version of the objects in Screen3 while saving the modifications. If you would also modify data in Screen2 and save, the whole application transaction won't be atomar anymore (no automatic rollback, you would have to do that yourself).

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 9:17 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:49 am
Posts: 21
Thanx Christian

So if I want to write to the db in screen 1 or 2 I should use the session per request with detatched objects.

I have another question..
If i want to associate an object already in database, to a new object, will i have to save the new object to database before i make the association?

I'll explain closer:


/**
* @hibernate.class
*/

public class Person() {

private Long personKey;
...
private Set reportedAccidents = new HashSet();

/**
* @hibernate.id generator-class="native" unsaved-value="null"
*/

public Long getPersonKey() {
return personKey;
}

public void setPersonKey(Long _personKey) {
personKey = _personKey;
}

...

/**
* @hibernate.set lazy="true" inverse="true"
* @hibernate.collection-key column="reportedBy"
* @hibernate.collection-one-to-many * class="edu.ncsystem.Nonconformance"
*/
public Set getReportedAccidents() {
return reported;
}

public void setReportedAccidents(Set set) {
reported = set;
}

public void addReportedAccidents(Accident accident) {
reported.add(accident);
}
}

/**
* @hibernate.class
*/

public class Accident() {
private Long accidentKey;
...
private Person reportedBy;

/**
* @hibernate.id generator-class="native" unsaved-value="null"
*/

public Long getAccidentKey() {
return personKey;
}

public void setAccidentKey(Long _personKey) {
personKey = _personKey;
}

.......

/**
* @hibernate.many-to-one
*/
public Person getReportedBy() {
return reportedBy;
}

public void setReportedBy(Person _reportedBy) {
reportedBy = _reportedBy;
}
}

Lets say a person object is in db, and i will register a new accident. I retrieve a person object from db and associates it with my new accident object, wich is not yet saved to db. Then i'll get a net.sf.hibernate.TransientObjectException right?
I wish it was possible to associate an object in database to a object not yet in database so I could use the session per application transaction pattern.
Or is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 9:29 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Sure, specify a "cascading" option for the Set and cascade the save and/or update operations.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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