-->
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.  [ 8 posts ] 
Author Message
 Post subject: Multiple session.save() ?
PostPosted: Mon Apr 26, 2004 4:07 am 
Beginner
Beginner

Joined: Fri Apr 02, 2004 3:34 am
Posts: 40
In one transaction, is it safe to do multiple sessin.save(obj) ? Of course, each time, with a different object.

Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Of course it is.


Top
 Profile  
 
 Post subject: Multiple session.save() in 1 Transaction
PostPosted: Mon Apr 26, 2004 4:13 am 
Beginner
Beginner

Joined: Fri Apr 02, 2004 3:34 am
Posts: 40
Of course :-), but I keep getting the below exceptions. In essence, I do the following:

1. Employee e = new Employee();
2. TreeSet ts = new TreeSet();
3. ts.add(new Office())...blabala ad infinitum.
4. e.setOffices(ts);
5. session.save(e);
6. Obtain an office iterator from e and for each item do a session.save(office);

I use MySQL and autoincrement and "native" id. After 5, I print the id, and indeed, there is a new object id, but then the below happens...


004-04-26 01:07:06,566 ERROR http8080-Processor22 net.sf.hibernate.JDBCException: Could not execute JDBC batch update
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)


Caused by: java.sql.BatchUpdateException: Deadlock found when trying to get lock; Try restarting transaction, message from server: "Lock wait timeout exceeded; Try restarting transaction"


michael wrote:
Of course it is.


Top
 Profile  
 
 Post subject: Re: Multiple session.save() in 1 Transaction
PostPosted: Mon Apr 26, 2004 4:14 am 
Beginner
Beginner

Joined: Fri Apr 02, 2004 3:34 am
Posts: 40
For each Office o, I am also doing a o.setEmployee(e)...


Acco Lade wrote:
Of course :-), but I keep getting the below exceptions. In essence, I do the following:

1. Employee e = new Employee();
2. TreeSet ts = new TreeSet();
3. ts.add(new Office())...blabala ad infinitum.
4. e.setOffices(ts);
5. session.save(e);
6. Obtain an office iterator from e and for each item do a session.save(office);

I use MySQL and autoincrement and "native" id. After 5, I print the id, and indeed, there is a new object id, but then the below happens...


004-04-26 01:07:06,566 ERROR http8080-Processor22 net.sf.hibernate.JDBCException: Could not execute JDBC batch update
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)


Caused by: java.sql.BatchUpdateException: Deadlock found when trying to get lock; Try restarting transaction, message from server: "Lock wait timeout exceeded; Try restarting transaction"


michael wrote:
Of course it is.


Top
 Profile  
 
 Post subject: Re: Multiple session.save() in 1 Transaction
PostPosted: Mon Apr 26, 2004 4:33 am 
Beginner
Beginner

Joined: Fri Apr 02, 2004 3:34 am
Posts: 40
And there is no cascade set in the .hbm files.

It's just a simple one-to-many and I can't get it to work..:-(

Employee ---*-> Office




Acco Lade wrote:
For each Office o, I am also doing a o.setEmployee(e)...


Acco Lade wrote:
Of course :-), but I keep getting the below exceptions. In essence, I do the following:

1. Employee e = new Employee();
2. TreeSet ts = new TreeSet();
3. ts.add(new Office())...blabala ad infinitum.
4. e.setOffices(ts);
5. session.save(e);
6. Obtain an office iterator from e and for each item do a session.save(office);

I use MySQL and autoincrement and "native" id. After 5, I print the id, and indeed, there is a new object id, but then the below happens...


004-04-26 01:07:06,566 ERROR http8080-Processor22 net.sf.hibernate.JDBCException: Could not execute JDBC batch update
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)


Caused by: java.sql.BatchUpdateException: Deadlock found when trying to get lock; Try restarting transaction, message from server: "Lock wait timeout exceeded; Try restarting transaction"


michael wrote:
Of course it is.


Top
 Profile  
 
 Post subject: Multiple session.save()...
PostPosted: Mon Apr 26, 2004 11:28 am 
Beginner
Beginner

Joined: Fri Apr 02, 2004 3:34 am
Posts: 40
So, Employee --*> Office.

Office holds a foreign key to Employee and in the Office POJO, one can setEmployee(). In the Employee POJO, one can setOffices().

1. Employee e = new Employee();
2. Set tsOffices = new TreeSet();
3. Office o = new Office();
3. tsOffices.add(o);
4. e.setOffices(tsOffices)
5. o.setEmployee(e);

6. session.save(e);
7. session.save(o);

Won't work!!! I keep getting deadlock. HOWEVER, if I don't do step 4, then it works. Can someone explain what I am doing wrong? If I can't do step 5, then Hibernate is a major PITA to use.

This is againsta MySQL. The Employee table has a PK that is auto-incremented. And the Office table has a employeeId which is a foreign key poiting to the Employee table. The Office POJO has no employeeId, only a field to setEmployee().

Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 11:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Do you do proper transaction handling with the hibernate Transaction API or with JTA?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 11:16 pm 
Beginner
Beginner

Joined: Fri Apr 02, 2004 3:34 am
Posts: 40
I do. Anyway, the next day, I restarted all the machines, re-run the same code, and it all worked...??!??

I am happy...

michael wrote:
Do you do proper transaction handling with the hibernate Transaction API or with JTA?


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