473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

JDBC Connection

303 Expert 100+
This worked fine the first time I ran it. The next day, it couldn't connect. It looks like MySQL drops the connection during the night, and then I can't get it back. My question is, is there something wrong in my code, or is the error due to the way the server handles the connection?
Expand|Select|Wrap|Line Numbers
  1. //code snippet
  2. public class MyClass{
  3.   InitialContext ic;
  4.   DataSource ds;
  5.   Connection conn;
  6.   PreparedStatement stmt;
  7.   ResultSet rs;
  8.  
  9.   public void Init(){ //called when application initializes
  10.     try{
  11.         ic = new InitialContext();
  12.         ds = (DataSource)ic.lookup("jdbc/MyDB"); //MySQL db
  13.     } catch (Exception e){log(e);}
  14.   }
  15.  
  16.   public void Exit(){ //called when application closes
  17.     closeAll();
  18.   }
  19.  
  20.   public void closeAll(){
  21.     try{
  22.         if(rs != null){rs.close();}
  23.         if(stmt != null){stmt.close();}
  24.         if(conn != null){conn.close();}
  25.     }
  26.     catch(SQLException e){log(e);}
  27.     catch(Exception e){log(e);}
  28.   }
  29.  
  30.   public void loadIDs(){ //called by user action
  31.     try{
  32.         conn = ds.getConnection();
  33.         stmt = conn.prepareStatement("SELECT id FROM mytable WHERE id > ?;");
  34.         stmt.setInt(1, x); //x comes from elsewhere
  35.         rs = stmt.executeQuery();
  36.         while(rs.next()){/* list ids */}
  37.     }
  38.     catch(SQLException e){log(e);}
  39.     catch(Exception e){log(e);}
  40.     finally{closeAll();}
  41.   }
  42.   // other methods access database similar to loadIDs
  43. }
Expand|Select|Wrap|Line Numbers
  1. Partial error:
  2. No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:
  3. ** BEGIN NESTED EXCEPTION ** com.mysql.jdbc.CommunicationsException MESSAGE: Communications link failure due to underlying exception:
  4. ** BEGIN NESTED EXCEPTION ** java.net.SocketException MESSAGE: Software caused connection abort: socket write error
Jul 13 '11 #1
0 1514

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sugapablo | last post by:
I have someone who has a proprietary database. They said that the only way to access the DB is via a JDBC connection. Now, does this mean I HAVE to use JAVA or JSP to connect to the DB? Does...
1
by: Kona | last post by:
Hello, I'm new with Oracle JDBC connection. Is it a way to crypt, hide the password string into a Oracle JDBC connection string ? The goal is to hide the password within script file. Thank's...
5
by: Steffen | last post by:
hi there, i have a problem connecting to a db2 database 7.2.4 on AIX using java when running the javacode on AIX using jdk1.3.1. The classpath is correctly set to java12/... on windows the...
1
by: Andrea | last post by:
Hello, I spent now several hours searching the google groups without finding an solution. I am kind of Newbie to DB2 and JSP and therefore working with JSP4Dummies (not sure whether I should...
2
by: apattin | last post by:
Hi all, Does anyone know how to get the schema name given a JDBC Connection object? I see that I can do "toString" and pick the "User=" substring, but smells of a hack. :) Thanks, ...
1
by: doniravi | last post by:
hello sir... i m using sql server 2000 database and usig jsp to access it when accessig i am getting followig error java.sql.SQLException: Unable to connect. Invalid URL. i installed...
3
by: %NAME% | last post by:
When using embedded SQL for db2 under solaris, you do not need to give username and password since the system will use your unix-login as default credential. However, with JDBC seems I always need...
0
by: SagittaDeveloper | last post by:
Hi, I'm have a problem connecting to IBM Universe via JDBC. When I run the connection code to UV from a none web application it connects and returns data. When I use the same connection code...
2
by: gollamudi | last post by:
1.what is the connectionpool in jdbc connection? 2.diffrence between normal jdbc connection and connectionpool jdbc connection?
1
by: Debabrata Jana | last post by:
Please go through the JAVA code below: class Main{ public Connection getConnection(){ Connection l_con = null; try { String hostname = "172.10.23.1:1527"; //Host...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.