LazyList and DB Connection

82 views
Skip to first unread message

Alexander Muravya

unread,
Nov 19, 2012, 12:39:07 AM11/19/12
to activejd...@googlegroups.com
I faced with a bit a problem with active JDBC in my code.

I opened connection then I do query:
LazyList<Staff> allRecords = Staff.findAll().limit(1); - where Staff is Model

after this I close connection and call other method passing allRecords as param. 
And if do something like: System.out.println(allRecords.toString()); in other method. It will fail with DBException (Are you sure you opened connection? Query: SELECT * FROM Stuff LIMIT 1.)

At this moment I found workaround. I call other method and after getting results I close connection.

But could explain me why I need connection after I made query and stored it as LazyList?

igor

unread,
Nov 19, 2012, 12:44:27 AM11/19/12
to activejd...@googlegroups.com
yes, this is expected behavior, although documentation for this could improve. 

Basically, this is a LazyList - a really lazy one :)

So, when you call this:
LazyList<Staff> allRecords = Staff.findAll().limit(1);
there is no access to the DB. What is happening here is that you create an instance of LazyLlist and you configure it for a future call with a query and other methods. 

However, when you try to call any method to read data from the list, /then/ it queries the DB and pulls all data locally

hope this helps,

igor

bryan p

unread,
Feb 20, 2015, 2:35:45 PM2/20/15
to activejd...@googlegroups.com
Igor,

After reading this and the docuemtation here http://javalite.io/lazy_and_eager, I want to make sure I understand the db accesses. I have the follwoing code:

public static boolean verifyUserMerchant(Account user,  Long id) {

       
for (Merchant m : user.getMerchants()) {
           
if(m.getLongId().equals(id)) {
               
return true;
           
}
       
}        
       
       
return false;
   
}

// which references this class

class Account()
{
...

public List<Merchant> getMerchants() {return getAll(Merchant.class);}

}


So would this foreach loop make one all to the db to get all merchants, or will it make a db call every iteration. I'm thinking the latter and poked through the code briefly but didn't see
where your actually making the db calls.  

Igor Polevoy

unread,
Feb 20, 2015, 3:16:43 PM2/20/15
to activejd...@googlegroups.com
Bryan - the system will make only one call to the DB. You can trace all calls to DB if you enable  logging: http://javalite.io/logging

thanks

bryan p

unread,
Feb 20, 2015, 5:21:10 PM2/20/15
to activejd...@googlegroups.com
Thanks Igor. 

Reread the documentation and now I see, it doesn't cache the association in the calling class so everytime you cal getAll() it will hit the db again. 
So in my case all the Merchant's will be returned in one db call and stored in a List since I only call getAll() once.    

Though I must have something configured incorrectly with slf4j and log4j, as I have it set to debug logging and don't see any of the activejdbc logging yet
see all of my debug logs.
 
Maybe I'll post another topic about that. 

Igor Polevoy

unread,
Feb 20, 2015, 5:55:51 PM2/20/15
to activejd...@googlegroups.com
yeah, log4j is probably writing this into a file. Just use slf4j-simple to see output. 

tx
Reply all
Reply to author
Forward
0 new messages