is there a way I can re-use the java resultset object?
I am trying to get the data from DB, create a object Map out of it and at the same time need to generate a .csv file. If I pass the same resultset to the method that creates the .csv file I get error. re-executing the same query twice will cost performance issue.
Are the performance issues that big? Is it a huge query? Or are you doing premature optimization?
If your database driver supports it, the resultSet.beforeFirst() method might be handy.
Can your CSV be produced from the Map you have in memory rather than the ResultSet? i.e. if it already has all the info you need, why do you even need to read it twice?
Or you could refactor your code so that instead of each method taking a result set and scrolling through it, they should take a result set row.
That way you only need to iterate through the result set once, calling both methods from that.
That is slightly more complicated though. I would prefer to keep the code simple even if there is a slight performance penalty.
I am never comfortable of passing the ResultSet here and there.
I always iterate through ResultSet once, store the result in a List or whatever Collection is suited for the result.
Then I pass that Collection object to every method if needed.
I do not know how experienced people think about it, but I feel comfortable in releasing the java.sql objects as soon as possible.
Actually I saw a lot of open cursor issues in the application when I started my career as a programmer.
May be that has injected a fear of the Sql obejcts in me.
So I am desperate to close them as soon as possible.
But currently the application which I am working on is built in Spring MVC-Hibernate.
So getHibernateTemplate() came to my rescue (though it can not be used in all scenarios) and I am free from Sql-object-fobia.
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad: