-->
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: Not able to connect issue
PostPosted: Mon Oct 06, 2008 3:49 pm 
Newbie

Joined: Mon Oct 06, 2008 10:40 am
Posts: 12
I think I may know the reason for this breaking, but I wanted to confirm it.

I have the following code (stripped of course):
Code:
Session session = sessionFactory.openSession();

Query query1 = session.getNamedQuery("findAllAccounts");
List someList = query.list();
Transaction tx = session.beginTransaction();

for (Iterator<SomeAccount> it=l.iterator(); it.hasNext(); ) {
    SomeAccount account = it.next();
    ...
}


The above code finds all accounts in a temp table called TEMP_ACCOUNTS. Now what I need to do is check if a value from this temp table is in another table. So I did (inside the for loop above):
Code:
Query query2 = session.getNamedQuery("findAllAccounts2");
List someList2 = query2.list();


So essential I have:
Code:
Session session = sessionFactory.openSession();

Query query1 = session.getNamedQuery("findAllAccounts");
List someList = query.list();
Transaction tx = session.beginTransaction();

for (Iterator<SomeAccount> it=l.iterator(); it.hasNext(); ) {
    SomeAccount account = it.next();
    ...
    ...
    ...
    Query query2 = session.getNamedQuery("findAllAccounts2");
    List someList2 = query2.list();
}


At the last line above, I get the two following errors:
Not able to obtain connection and
Session is closed.

I also tried to create a new Session to get the "findAllAccounts2" query, but it still happens.

Is this because I started the transaction above using the first query and trying to use two different queries within this transaction?

Any direction on this would be great! Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2008 4:47 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The are some strange things in the code you posted. Maybe it is just a copy/paste error...

1. You start the transaction 'tx' after the first query. I really don't know how this affects things, but I would move the session.beginTransaction() call to the line just after sessionFactory.openSession().

2. The first query returns list 'someList', but you are iterating list 'l'. Typo?

In any case, having two or more queries in a single transaction should work just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2008 8:02 am 
Newbie

Joined: Mon Oct 06, 2008 10:40 am
Posts: 12
You're right... it was a typo. The code should have been:
Code:
Session session = sessionFactory.openSession();

Query query1 = session.getNamedQuery("findAllAccounts");
List someList = query.list();
Transaction tx = session.beginTransaction();

for (Iterator<SomeAccount> it=someList.iterator(); it.hasNext(); ) {
    SomeAccount account = it.next();
    ...
    ...
    ...
    Query query2 = session.getNamedQuery("findAllAccounts2");
    List someList2 = query2.list();
}


I will try moving the session.beginTransaction() right after getting the session. Thank you for your time


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2008 8:03 am 
Newbie

Joined: Mon Oct 06, 2008 10:40 am
Posts: 12
You're right... it was a typo. The code should have been:
Code:
Session session = sessionFactory.openSession();

Query query1 = session.getNamedQuery("findAllAccounts");
List someList = query.list();
Transaction tx = session.beginTransaction();

for (Iterator<SomeAccount> it=someList.iterator(); it.hasNext(); ) {
    SomeAccount account = it.next();
    ...
    ...
    ...
    Query query2 = session.getNamedQuery("findAllAccounts2");
    List someList2 = query2.list();
}


I will try moving the session.beginTransaction() right after getting the session. Thank you for your time


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.