Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS SQL Server and JDBC: closed connection

Im getting

I/O Error: DB server closed connection.

while connecting to MS SQL server 2008 from java code .


SQL server is in mixed mode and its in local machine.My connection string is jTDS

jdbc:jtds:sqlserver://machineName:1433;databaseName=DB;integratedSecurity=true


stack trace is

java.sql.SQLException: I/O Error: DB server closed connection. at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2311) at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:610) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(ConnectionJDBC2.java:345) at net.sourceforge.jtds.jdbc.ConnectionJDBC3.(ConnectionJDBC3.java:50) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.app.hibernate.test.(test.java:22) at com.app.hibernate.test.main(test.java:53) Caused by: java.io.IOException: DB server closed connection. at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(SharedSocket.java:848) at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(SharedSocket.java:727) at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(ResponseStream.java:466) at net.sourceforge.jtds.jdbc.ResponseStream.read(ResponseStream.java:103) at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2206) ... 8 more Exception in thread "main" java.lang.NullPointerException at com.app.hibernate.test.db(test.java:36) at com.app.hibernate.test.main(test.java:54)

JDBC Driver

String url ="jdbc:sqlserver://machine:1433;instance=SQLEXPRESS;databaseName=db";

stacktrace

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'username'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:156) at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:240) at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:78) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2636) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2046) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2034) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4003) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1550) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1207) at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.app.hibernate.test.(test.java:22) at com.app.hibernate.test.main(test.java:53) Exception in thread "main" java.lang.NullPointerException at com.app.hibernate.test.db(test.java:36) at com.app.hibernate.test.main(test.java:54)

like image 772
Mukthi Avatar asked Jun 27 '12 11:06

Mukthi


People also ask

How do you deal with closing connections in database pool?

SOLUTION. To resolve this issue, it is necessary to configure a datasource for JDBC that accepts a connection checking mechanism. Before being used, a connection is tested for its validity and not used if it has been closed already. An implementation of either c3p0 or Apache Commons DBCP is recommended.

Can JDBC connect to SQL Server?

Connect to SQL Server Using JDBC Driver and Command Line Or, to connect without Windows authentication, use the configured JDBC data source and specify the user name username and the password pwd . For example, this code assumes that you are connecting to a JDBC data source named MSSQLServer .

What is Java SQL Sqlrecoverableexception closed connection?

It means the connection was successfully established at some point, but when you tried to commit right there, the connection was no longer open. The parameters you mentioned sound like connection pool settings. If so, they're unrelated to this problem.

How do you fix TCP Provider An existing connection was forcibly closed by the remote host?

An existing connection was forcibly closed by the remote host' error SOLUTION: The fix is to update the Microsoft SQL 2012 Native client to a higher version on the AOS. Download Microsoft SQL Server 2012 Native Client – QFE and install it on the AOS server to resolve the issue.


1 Answers

Your Connection String and authentication have errors. if it is mix mode don't use SQL authentication

Try this

PC Name : janaka-pc SQL User Name : sa SQL Password
: 1234 Database : Janak_DB

Code for sql Conncetion in JDBC

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://janaka-PC;user=sa;password=1234;database=Janak_DB");

like image 163
janaka aravinda Avatar answered Oct 06 '22 00:10

janaka aravinda