-->
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.  [ 2 posts ] 
Author Message
 Post subject: Bact insert error
PostPosted: Fri Jul 15, 2005 8:01 am 
Beginner
Beginner

Joined: Thu Mar 24, 2005 6:29 am
Posts: 24
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.0


Hi, i am using Hibernate with Tomcat 5 and using HibernateUtil
git from caveat Emptor when adding data i some times get session closed error the code below written is for batch insertion
but when doing this operation i am getting the error session is closed



plz go through the code and tell me is there any problem in that
because i have been stuck with this error regularly .



public static boolean createCoordinators(String catid, int[] userids,
boolean recurse)
{
System.out.println("printing from create coordinators");
String[] tempIds = null;
String[] categoryids = null;
CategoryReviewer categoryReviewer = null;
if(recurse)
{
try{
tempIds = getSubCategoriesId(catid);
}catch(CategoryNotFoundException ex)
{
ex.printStackTrace();
return false;
}
categoryids = new String[tempIds.length+1];
categoryids[0] = catid;
for(int k = 0 ; k < tempIds.length ; k++ )
{
categoryids[k+1] = tempIds[k];
}

}
else{
categoryids = new String[1];
categoryids[0] = catid;
}

HibernateUtil.beginTransaction();
try{

for(int j=0 ; j < categoryids.length ; j++)
{ System.out.println("printing from first forloop");
for(int i= 0 ; i < userids.length ; i++)
{
System.out.println("Printing from Second loop");
categoryReviewer = new CategoryReviewer();
categoryReviewer.setId(UniqueIdGenerator.getUniqueId());
categoryReviewer.setCategoryId(categoryids[j]);
categoryReviewer.setReviewerId(userids[i]);
HibernateUtil.getSession().save(categoryReviewer);


}

}
HibernateUtil.commitTransaction();
}catch(HibernateException ex)
{
ex.printStackTrace();
return false;
}
finally{

HibernateUtil.closeSession();
}

//TODO if this session.close is uncommented then it will show exception Session.closed










return true;
} // end createCoordinators





Thanks in advance

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 8:11 pm 
Newbie

Joined: Fri Jul 15, 2005 6:50 pm
Posts: 3
Location: Bay Area, California,USA
The HibernateUtil.save function opens and closes the session associated with the threadlocal. So solution to you problem would be

[b]HibernateUtil.beginTransaction(); [/b]
try{

[b]Session session = HibernateUtil.getSession(); [/b]

for(int j=0 ; j < categoryids.length ; j++)
{ System.out.println("printing from first forloop");
for(int i= 0 ; i < userids.length ; i++)
{
System.out.println("Printing from Second loop");
categoryReviewer = new CategoryReviewer();
categoryReviewer.setId(UniqueIdGenerator.getUniqueId());
categoryReviewer.setCategoryId(categoryids[j]);
categoryReviewer.setReviewerId(userids[i]);
[b]session.save(categoryReviewer); [/b]
}

}
[b]HibernateUtil.commitTransaction(); [/b]
}catch(HibernateException ex)
{
ex.printStackTrace();
[b]HibernateUtil.rollbackTransaction(); [/b]
return false;
}
finally{
[b]HibernateUtil.closeSession(); [/b]
}


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