views:

30

answers:

2

we have been trying to benchmark our application performance in multiple way for sometime now. I always believed that object creation in java using Class.newInstance() was not slow (at least after 1.4 version of java). But we anyways did a test to use newInstance method vs mainitain an object pool of 1000 objects. We did about 200K iterations of loading data from DB using JDBC and populating these objects. I was amazed (even shocked) to see that newInstance code compared to object pool code was almost 10 times slower.

These objects represent tables with about 50 fields and all string type.

Can someone share there thoughts on this issue as now I am more confused if object pooling of atleast some DAO instances is a better option. The pool size as I see right now should be large enough to meet size of average requests. There is a flip side as my memory footprint will go up but I am beginning to wonder if this kind of idea makes sense atleast for some of the DAO entities representing tables of about 50 or more columns

Please share your ideas and let me know if this has been tried by someone or am I missing some point here

A: 

To quote from Java Concurrency in Practice:

Just say no to object pools

There is likely a simpler option to whatever problems you are having. For instance, why are you instantiating so many instances of your DAOs in the first place? Is a new DAO instance being created every time you need to retrieve data from the database?

Why not just re-use the same instance of the WhateverDao each time you need access to the Whatever data? Just make sure that the DAO is thread-safe.

matt b
I am not sure I understand your solution. Yes every time I access the DB, there is a generic code which based on class name creates a new instance. If you are suggesting reusing already created instances, that is exactly what I want to do with the object pool. Please let me know what am I missing?
Fazal
I am saying that creating a pool is a bad idea. The classes that use the DAO should have a member variable referring to the DAO it needs; it shouldn't need to ask for a new DAO reference each time it needs to make an operation.
matt b
A: 

A pool of DAO objects is a bad idea. A pool of database connections, is a good idea. My question is, which do you really have? If you are opening a connection in each DAO, without pooling the connections, then you're seeing better performance because you're pooling the connections, not because you're pooling the DAO objects. My next question is, what are you doing in the constructor that's taking so long? If you're constructors are expensive to execute, then the object pool may be a good idea. Although, I'd really look at getting whatever you're doing out of the constructor...

Jim Barrows
We have a pool of Database connections. The code to open connection is not each DAO but one super class for all DAO objects. After getting the result set on query execution, this code instantiates the object of the right subclass using CLass.newInstance() method and then a method is called in the subclass which reads the result set and populates all datamembers of the objects which represent all fields in the table
Fazal