2

I am connecting to MS SQL via hibernate using the jar jtds-1.3.0.jar and below is the configuration file

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.url">jdbc:jtds:sqlserver://localhost/login</property>
    <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
    <property name="connection.username">sa</property>
    <property name="connection.password">user</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>


    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Echo all executed SQL to stdout -->

        <property name="show_sql">true</property>
    <!--  Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

    <!-- configuration pool via c3p0--> 
    <property name="c3p0.acquire_increment">5</property> 
    <property name="c3p0.idle_test_period">100</property> <!-- seconds --> 
    <property name="c3p0.max_size">20</property> 
    <property name="c3p0.max_statements">50</property> 
    <property name="c3p0.min_size">5</property> 
    <property name="c3p0.timeout">1800</property> <!-- seconds --> 
    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <!--Basic user functionality-->

</session-factory>

but every time i run my project its giving error as Network error and connection refused. I refereed this link for the still giving errors. Below is my stack trace

java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:434)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:183)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:183)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:172)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:152)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1074)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1061)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1796)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:635) Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.reflect.GeneratedMethodAccessor24.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:300)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:253)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:329)

below is the image Server Configuration Manager can anyone tell me where i am going wrong.

Community
  • 1
  • 1
Rithesh
  • 135
  • 1
  • 2
  • 11

4 Answers4

4

Are you sure that your server is listening on port 1433? To confirm that the actual problem is with Java (i.e. your configuration) run

telnet localhost 1433

If you get no answer then MS SQL is most likely not running on 1433. There is an option to use dynamic ports in MS SQL, make sure you didn't enable that.

http://frightanic.com/software-development/connecting-to-ms-sql-server-2012-express-through-jdbc-failed/:

The first hurdle was to learn that MS SQL Express by default uses dynamic ports. To connect in a TCP/IP fashion from Java you need to configure static ports manually.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • Hi Marcel, i created db in Microsoft SQL server Management Studio does it make any difference?? – Rithesh Feb 11 '13 at 13:13
  • Hi @Marcel Stor need to run telnet localhost 1433 in command prompt or ? – Rithesh Feb 11 '13 at 13:22
  • @Rithesh, I don't know. Did you read the post I linked and the Microsoft article it points to (http://msdn.microsoft.com/en-us/library/ms177440.aspx)? – Marcel Stör Feb 11 '13 at 13:41
  • yes i read that also gone through also with [this](http://knowhow.visual-paradigm.com/hibernate/solving-sql-server-connection-problem/) link but nothing worked – Rithesh Feb 11 '13 at 13:44
  • @Rithesh, you still haven't reported whether connecting with telnet works and if you're using dynamic or static ports. – Marcel Stör Feb 12 '13 at 06:15
  • Hi @MarcelStör i have updated the image there was no 0 in dynamic ports – Rithesh Feb 12 '13 at 07:54
2

Yes portNumber is Optional . The default is 1433. If you are using the default, you do not have to specify the port, nor its preceding ':', in the URL.

<property name="connection.pool_size">10</property>

It will allow only one connection at a time .I guess some where in your program you are trying to open another session .

Have a look on

Hibernate config connection pool size

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • hi suresh ya i have given the port number and increased the pool size to 10. but still i think there is problem in connecting to SQL Server – Rithesh Feb 11 '13 at 12:28
  • Is everything fine like this link http://knowhow.visual-paradigm.com/hibernate/solving-sql-server-connection-problem/ – Suresh Atta Feb 11 '13 at 13:25
  • Hi suresh i have gone through that link, enabled the telnet was not connecting to server then changed the security to SQL Server Authentication still the same result. – Rithesh Feb 11 '13 at 13:46
  • Even though its optional give port as 1433 and try once. – Suresh Atta Feb 11 '13 at 14:04
  • And make sure SQL Server Browser service needs to be running (in Services). – Suresh Atta Feb 11 '13 at 14:05
  • And one more thing you have to restart the SQL browser after enable the TCP/IP property – Suresh Atta Feb 11 '13 at 14:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24364/discussion-between-rithesh-and-the-suresh-atta) – Rithesh Feb 12 '13 at 09:44
2

try changing the url to:

<property name="connection.url">jdbc:jtds:sqlserver://localhost:1443;DatabaseName=login</property>

separating the schema name from the server address.

I use DBVisualizer to connect to MS Sql and it show the format of the url config:

URL Format: jdbc:jtds:sqlserver://<server>:<port1443>;DatabaseName=<database>
Euclides Mulémbwè
  • 1,677
  • 11
  • 18
1

I found the my solution to the problem. I changed my port number from 1433 to 1434 which was active by referring this discussion. Thank you all for your time

Community
  • 1
  • 1
Rithesh
  • 135
  • 1
  • 2
  • 11