• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Thread.sleep() with jdbc  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am uploading the data from a text file(which gets updated every 5 min.) into the database.

Can anybody tell me whether the approch I am following is efficient or is there any other way to achieve this ?

My approach is :
1. Getting a connection object ONCE from the database.
2. Putting statement.executeUpdate() in an infinite loop.

This is working fine without any problems till now. The only problem which I foresee is that when the program is stopped, the connection and statement objects are not being closed.(OR are they closed automatically ?). If they are not closed, how to close the connection objects when the user types 'ctrl+c' to exit the program ?

Following is the sample code..

public class JdbcThread{
public static void main(String[] args){
for(int i=0;i>=0;i++){ //i.e., infinite loop
try{
//read values from the text file..
int k=20; //the total no. of lines in the text file.
for(int j=0;j<k;j++){
st.executeUpdate("INSERT INTO TEMP(ID,VALUE,QUALITY) VALUES(..)");
Thread.sleep(18000000);//sleep for 5 min.
}
}catch(java.lang.InterruptedException ie){System.out.println(ie.getMessage());}
}

}
}
 
Marshal
Posts: 67197
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JDBC.
 
author & internet detective
Posts: 39073
714
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darsi,
No, the connection and statement objects are not closed automatically. As an aside, I recommend using a PreparedStatement rather than a Statement since the SQL is similar each time. This will provide a performance boost.

While the connection and statement objects aren't closed, it probably isn't a big deal because the JVM is being exited. It is cleaner to clean up though, which can be done by exiting in a more controlled manner. For example, if you exit by having the user enter a value (and check for it on each iteration) or having the user modify a file, you can close the connection and statement yourself.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May, be in the catch (InterruptedException e)
{
// close the statement
// return the connection to the pool
}
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!