I create an object in my servlet when inserting into my Oracle 9i database where the insertData method is using PreparedStatement. My insertData method has closing statements in finally block. There are about 5 records input in the database per day.
I was also wondering about using static insertData method instead of creating object. Please advise what the negatives are for the way I am doing it compared to using a static method to insert database info. [ September 25, 2008: Message edited by: Ed Carrington ]
The short answer is if it is working as it is required to work for you, then it is the right way.
I would speculate that having the handler that does the insert is not as important if it is static or in an object each invocation, so much as how this handler internally works with the getting of the database connection handle and how it internally cleans up the connection object (releases the prepared statement and the connection).
From the java VM point of view, creating a new class instance every invocation, vs having a static method, where by either gets a connection from (a jndi bound data source?), does a jdbc insert statement, closes it, over time would likely be about the same. Though there might be slightly better performance from not having to create a new instance of the handler class each and every invocation, but that likely won't be the case if there are only a few created every day.
now for ease of extensibility or code reuse, it might be advantageous if the accessor was not a static method, but a class instance with a regular instance method. this could allow for future subclassing and overriding or extending the save() method to add new hooks so that additional things can be done when ever something is saved.
When using other frameworks, like spring, to wire up a handler class as a service, where you define a java class to be the data accessor and then 'wire' it up as a service via the bean factory, it really bahaves more like a static method, since the class instance for the data accessor service is usually instantiated when the spring container boots up, and then remains alive, and useable by your application (assuming your build the accessor bean in a thread safe manner) until it is shut down. though here we have the ability to decouple the datasource from the data accessor class.
I can post more examples on this if that would be helpful.
Error: Keyboard not attached. Press F1 to continue.
Joined: Oct 11, 2007
Thanks for all your info! I am using direct jdbc connection and not jndi bound data source. My current method creating a new instance does work great but was wondering if I ever use it (in future projects) for something that has 30 to 40 database inserts per hour I would have a problem with performance? [ September 27, 2008: Message edited by: Ed Carrington ]
Ed, 30-40 hits per hour is quite low. If you do have a problem with performance, the first step would be using a connection pool. That way less opening/closing resources would be done. The statements stay in your code. It's just that close() means "return to pool" rather than "close."
Originally posted by Jeanne Boyarsky: Ed, 30-40 hits per hour is quite low. If you do have a problem with performance, the first step would be using a connection pool. That way less opening/closing resources would be done. The statements stay in your code. It's just that close() means "return to pool" rather than "close."
Thanks, definitely not having performance issues using new instance method after trying it with 50 hits per hour. Main thing I wanted to know is my database insert method (with new instance) is an acceptable practice in Java. [ September 27, 2008: Message edited by: Ed Carrington ]
Sure, there's nothing wrong with using instance methods instead of class (static) methods. In fact there are reasons why you shouldn't use class methods in many cases. But that has nothing to do with JDBC in particular. If you want to debate the use of instance versus class methods you should post that question in one of the general Java forums here.
Joined: Oct 11, 2007
Thanks, you are all very helpful as always in Javaranch. I appreciate your info and time on this.