473,385 Members | 1,492 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 1513

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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.