SQL Server 2000 Driver for JDBC: Connection reset by peer

If you’re using the SQL Server 2000 Driver for JDBC and Commons DBCP and you either you see this error in your logs:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Connection reset by peer: socket write error

and / or your application stops functioning after your SQL Server is restarted or the connection between the your application and the SQL Server is terminated, then you need to add this to your JDNI setup:

<parameter>
  <name>validationQuery</name>
  <value>select $somecolumn FROM $sometable</value>
</parameter>

where the value is a query that returns at least one row from your database. According to the documentation, the validation query is:

“… invoked in an implementation-specific fashion to determine if an instance is still valid to be returned by the pool. It will only be invoked on an “activated” instance.”

In my testing, if I add the validationQuery parameter to my JNDI setup, restart the servlet container (in this case Tomcat), request a page and then stop SQL Server, an error is returned (as expected). After starting SQL Server and requesting another page, the application returns to normal usage (which doesn’t happen if the validationQuery parameter is not present).

This entry was posted in J2EE. Bookmark the permalink.

22 Responses to SQL Server 2000 Driver for JDBC: Connection reset by peer

  1. A simpler validation query that works for MS SQL Server is simply “SELECT 1″.

  2. I switched to the free jTDS driver which seems to work pretty well.

  3. Phil Wilson says:

    I’d echo Glen here – the jTDS driver is excellent.

  4. Sarveswara Rao says:

    Hi,

    We are using JBOSS application server with
    MS SQL server JDBC driver. We are getting the Connection Reset by peer problems. Can you please guide me how to include the

    validationQuery

    We are dependent on JBOSS datasource. JBOSS provides one xml file where we need to specify the datasourc name , jdbc driver,url, user id and password.
    Thanks for help
    Sarveswara Rao

  5. Flyinal says:

    Connection reset errors are caused by a number of things.
    We moved from the MS SQL Server driver to jTds and it helped but there were two other issues of greater importance to remove the error.
    First are the network interface cards in your windows server. Most of them have the transmit and receive buffers set to 128 or 256.
    You can set them to 1024 (either in the registry or in the properties of the network card from control panel)
    and this will eliminate about 1/3rd of the errors.
    the next thing you can do is to optimize the TCP-IP settings for Windows. Be default you are lucky to get a NIC that can sustain 1.5Mbps – even in a server platform like Dell or SuperMicro.
    This link will give you the details on what to set, and where:
    http://rdweb.cns.vt.edu/public/notes/win2k-tcpip.htm
    Once we set the TCPIP values and increased the buffers on our DB server and our app/web servers our throughput went up over 4 times and the errors went away.
    Lastly, you need to check your Java code.
    App servers run servlets on different threads, but the DBCP pool is a singleton.
    After you close your DB connection it gets returned to the pool (critical that you call ResultSet.close() and Connection.close() )
    and you need to set the connection object to NULL immediately.
    There is a doc in the Apache DBCP docs about why this is the case. I have found this is needed for any database pool using JDBC.
    With the TCP mods, and the change in our Java code, we have no more connection issues no
    matter what JDBC driver we have tried (and have tried 3 now!)

  6. I have the same problem

    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Connection reset by peer: socket write error

    I could solve my problem using the check-valid-connection-sql tag, addinng the next line to the *-ds.xml file

    %check-valid-connection-sql>select 1 %/check-valid-connection-sql>

    I’m using the MS SQL Server JDBC driver

  7. Peter Leonard says:

    Hi,
    Just now freshly install the tomcat 5. i don’t the connection pooling for sql server. how can i get the datasource in the jsp. let me know please.

  8. Rafiuddin K says:

    Is it Okay, by switching from “Ms-sql Native Driver” to “jtds ” in point of Performance…

    plz.., feed me back…….

  9. RJ Regenold says:

    thanks so much for this. we have been looking for a fix to this problem for months now and this worked perfectly. thanks!

    rj

  10. nagaraj says:

    I use Tomcat 5.0 application server , database MS SQl server 2000 using Sql Server driver for JDBC.

    Before I was using ODBC Driver.But after changing to JDBS sql server driver, Some of the queries are not running properly.They are shouwing for Syntax errors.

    the error is like, “syntax near the word ‘as’”.(the query is has a subquery getting the value using ‘as’ clause in MSSQL Server)

    plz give me the solution.asap,

    rgds,
    nagaraj.

  11. Anitha says:

    i’m working on Sql server 2000 which is the suitablr driver i need to be used please help me out

  12. santhakumar says:

    I am facing the JDBC Connection reset by peer: Socket write error,

    Sometimes module working fine, yesterday module tested in production server twice within 1 hr gap,

    First test successfully finished, but second one was not.

    The data insertion is only the problem, module may insert any number of records at single insertion.

    I am using Sql server 2000 server with Microsoft jdbc driver and
    JBoss server.

    Our db server and jboss server (Hardware) are highly configured.

  13. Hello Freind

    I am getting this error

    Error:
    [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    back

    Ip port allare correct

    Regards
    Rajesh Pandey
    Mail id :– rajesh@sixthquadrant.com
    Mobile :– +919811903737

  14. santhakumar says:

    Hi,

    The below error occurring with jboss 4 and sql server 2005 combination but perfectly working with sql server 2000.

    com.microsoft.sqlserver.jdbc.SQLServerException: equal .
    12:59:04,079 INFO [STDOUT]

    message : equal .
    State : S0001
    err code : 50000

    My java code is below :

    public Object cancelChargeRun( String chargerun , String type){
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    Boolean ret = Boolean.FALSE;
    int i1 = -1;
    int i2 = -1;
    int level = 0;
    try{
    System.out.println(“Cancelling the chargerun.”);
    System.out.println(“charge run : “+ chargerun);
    System.out.println(“type : “+ type );
    // String query1 = “UPDATE CNTRMOVEMENTDETAIL SET RUN = 0, CHARGERUN = ” WHERE CHARGERUN = ? “;
    String query2 = “UPDATE DETADVCHARGERUN SET STATUS = ‘CANCELED’ WHERE CHARGERUN = ? “;//C for canceled
    String query3 = “DELETE FROM LRPDEMURRAGEEDINOS WHERE CHARGERUN = ? “;
    String query4 = “DELETE FROM LINEDETENTIONADVANCEMENT WHERE CHARGERUN = ? “;

    try{
    level = 1;
    con = new lrp.serverutils.refs.ServerObject().getConnection();

    level = 2;
    pst = con.prepareStatement( query2 , ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_READ_ONLY );
    pst.setString( 1 , chargerun );

    level = 3;
    i1 = pst.executeUpdate();
    level = 4;
    if( i1 >= 0 ){
    pst.close();
    pst = con.prepareStatement( query3 , ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_READ_ONLY );
    pst.setString( 1 , chargerun );

    level = 5;
    i2 = pst.executeUpdate();
    pst.close();

    level = 6;
    if( i2 >= 0 ){
    pst = con.prepareStatement( query4 , ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_READ_ONLY );
    pst.setString( 1 , chargerun );

    level = 7;
    i2 = pst.executeUpdate();
    pst.close();
    }
    }
    System.out.println(“Line Container Detention: Charge Run Canceled.”);
    }catch( SQLException se ){
    System.out.println(“Level Reached : “+ level);
    System.out.println(“level is : “+level+”\nSqlException cancelchargerun in Container DetetnionSessionBean.\n”+se);
    this.mailException( “advancement”,chargerun, “level is : “+level+”\nSqlException cancelchargerun in Container DetetnionSessionBean.\n “, se );
    }

    if( i2 >= 0 )
    ret = Boolean.TRUE;

    return ret;
    } catch( Exception exp ){
    System.out.println(“Exception CancelChargeRun ContainerDetentionSessionBean.\n”+exp);
    return ret;
    }

    error occurring at level = 3 assignment.

    Hope any one help.

    Thanks & regards,
    Santhakumar S

  15. Bhanu says:

    I also faced the same problem and did the following :

    1. Rebooted the machine.
    2. open mssql-ds.xml and uncomment the tag..some arbitrary sql
    and give an sql which returns atleast one row from your database.
    It should work.

  16. Achint says:

    Hello every body
    when i m connecting throught “com.microsoft.jdbc.sqlserver.SQLServerDriver”
    then this Exception —–
    [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

    but if i use “odbc” then it works properly

    if any one of u have a solution for this prob then plz help me

    ThanKs

  17. ZsJoska says:

    Excellent hint! Applies also to SQL Server 2005 (jTDS)

  18. Elina says:

    i have created an applet already i signed that applet that applet is retriveing data from data base and showing result in Appletviewer through,it is retriveing data from ms acees.after signing the applet now it it is displaing at internet explorer but when i want to connect that applet MS SQL server 2000 it is only coming i appletviewer but not in internet explorer it is giving
    ava.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    Error ,i SQl server is in separate machine that machine port no 1433 is disable and everything is right why it is giviing this type of error plz tell me

  19. Help me says:

    Hai,
    I have got the error during doing jdbc programe
    the error is java.sql.SQLException[Microsoft][SQLServer 2000]Driver error establishing scoket

    and i was checked the tcp/ip proctol enabled it was working fine but i don’t know why i got this error plz help me

  20. hello
    plz can anybody tell me how to connect an applet with sql server 2000 .Can anyone plz give me the code for this.
    regards
    neelam(neelamdwivedi84@rediffmail.com)

  21. SoftWood says:

    i am configuring my appServer and this error stop my connection pool from pinging.

    An error has occurred.
    Operation ‘pingConnectionPool’ failed in ‘resources’ Config Mbean. Target exception message: Connection could not be allocated because: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

  22. Streamlong says:

    I’m getting the error. Please restart the database and the server,that’s ok.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>