简体   繁体   English

使用JDBC的SQL Server连接-JTDS

[英]SQL Server connection using JDBC - JTDS

I'm using the JTDS driver to communicate with the SQL Server and I'm having trouble managing the connection loss. 我正在使用JTDS驱动程序与SQL Server通信,并且无法管理连接丢失。 I am not able to check if connection is dropped. 我无法检查连接是否断开。 I will detail my problem: 我将详细说明我的问题:

This is my method that creates the connection: 这是我创建连接的方法:

public synchronized Connection NewConnection (ConnectionParameters params) {
         try {
             String url = "";
             if (params.getTipoBanco (). equals ("MySQL")) {
                 if (! mySqlDriverLoaded)
                     return null;
                 url = "jdbc:mysql://"+params.getIpServidor()+":"+params.getPorta()+"/"+params.getNomeBanco()+"?user="+params.getUsuario()+"&password="+params.getSenha();
             } else if (params.getTipoBanco (). equals ("SQL Server")) {
                 if (! sqlServerDriverLoaded)
                     return null;
                 url = "jdbc:jtds:sqlserver://"+params.getIpServidor()+":"+params.getPorta()+"/"+params.getNomeBanco()+";user="+params.getUsuario()+";password="+params.getSenha();
             } else {
                 throw new IllegalArgumentException("Type of database invalid data");
             }
             return DriverManager.getConnection(url);
         } Catch (Exception e) {
             e.printStackTrace ();
             message = "Error in connection:" + e.getMessage ();
             return null;
         }
     }

If the server is offline in creating the connection, you can check because a SQLException will be thrown. 如果服务器在创建连接时处于脱机状态,则可以检查是否将抛出SQLException。 The problem lies in the connection drop after it is successfully performed: 问题在于成功执行连接断开之后:

Connection connSqlserver = ConnectionFactory.getInstance().newConnection(paramSqlserver);
PreparedStatement stSqlServer = connSqlserver.prepareStatement("select * from operator");
ResultSet rsSqlserver = stSqlserver.executeQuery();

If the connection is dropped before or during the executeQuery(), the application gets locked in the method expecting the connection back. 如果在executeQuery()之前或期间删除了连接,则应用程序将锁定在期望返回连接的方法中。 No exception is thrown. 没有异常被抛出。

I've tried to use the following means to verify the connection drop: 我尝试使用以下方法来验证连接断开:

1 1

DriverManager.setLoginTimeout(x);

2 2

statement.setQueryTimeout(x);

3 loginTimeout=x parameter in the connection string 连接字符串中的3 loginTimeout = x参数

With none of these got success. 这些都不成功。

If you can give me some more guidance, will be grateful. 如果您能给我更多指导,将不胜感激。

Thank you! 谢谢!

FYI, LoginTimeout sets how long you wait to establish a connection. 仅供参考, LoginTimeout设置您等待建立连接的时间。 Once connected it has no effect. 一旦连接,将无效。 It looks like the jTDS drive implements socketTimeout which I think should cover your condition. 看起来jTDS驱动器实现了socketTimeout,我认为这应该可以满足您的条件。

They also implement a socketKeepAlive . 他们还实现了socketKeepAlive I am not familiar with how they implement it but I assume the poll the connection to either keep it open and\\or return an exception if it is dropped. 我不熟悉它们的实现方式,但是我假设对连接进行轮询以保持其打开状态,并且\\或者如果该连接被丢弃则返回异常。 I don't think jTDS offers this but some connection failover functionality might be useful as well. 我不认为jTDS提供此功能,但某些连接故障转移功能也可能有用。

A summary of one implementation 一个实现的摘要

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

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