简体   繁体   English

Hibernate MS SQL连接

[英]Hibernate MS SQL connection

I am connecting to MS SQL via hibernate using the jar jtds-1.3.0.jar and below is the configuration file 我使用jar jtds-1.3.0.jar通过hibernate连接到MS SQL,下面是配置文件

<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. 谁能告诉我哪里出错了

Are you sure that your server is listening on port 1433? 您确定您的服务器正在侦听端口1433吗? To confirm that the actual problem is with Java (ie your configuration) run 确认实际问题是使用Java(即您的配置)运行

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. 如果你没有得到答案,那么MS SQL很可能不会在1433上运行。有一个选项可以在MS SQL中使用动态端口,确保你没有启用它。

http://frightanic.com/software-development/connecting-to-ms-sql-server-2012-express-through-jdbc-failed/ : 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. 第一个障碍是了解MS SQL Express默认使用动态端口。 To connect in a TCP/IP fashion from Java you need to configure static ports manually. 要从Java以TCP / IP方式连接,您需要手动配置静态端口。

Yes portNumber is Optional . portNumber是可选的。 The default is 1433. If you are using the default , you do not have to specify the port , nor its preceding ':', in the URL . 默认值为1433.如果使用default ,则不必在URL指定port ,也不必指定其前面的“:”。

<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 Hibernate配置连接池大小

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: 我使用DBVisualizer连接到MS Sql,它显示url配置的格式:

URL Format: jdbc:jtds:sqlserver://<server>:<port1443>;DatabaseName=<database>

I found the my solution to the problem. 我找到了解决问题的方法。 I changed my port number from 1433 to 1434 which was active by referring this discussion. 我通过参考这个讨论将我的端口号从1433改为1434。 Thank you all for your time 谢谢大家的时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM