SQLServer数据库JDBC连接串参数的简单学习

发布时间 2023-12-13 13:35:59作者: 济南小老虎

SQLServer数据库JDBC连接串参数的简单学习


背景

前段时间一直跟同事一起处理SQLServer 比其他数据库的deadlock更多的问题. 
涉及到了几个驱动的参数. 想着问题基本上告一段落, 将这一块的情况总结一下.
便于后续遇到问题时的快速处理.

关于参数

现阶段的字符连接串为: 
jdbc:sqlserver://ip:port;database=dbname;SelectMethod=direct;sendStringParametersAsUnicode=false

主要是修改了这两个字符串:
SelectMethod=direct
以及
sendStringParametersAsUnicode=false

这两个参数的含义: 
1. SelectMethod 
当这个参数值为cursor时, 理论上数据库里面被跟踪到了:
FETCH API_CURSOR00000000000880BE
类型的SQL就会少很多. 
根据官网的解释, 这个参数是cursor 时, 会对大查询创建多个游标, 然后使用jdbc的client就可以每次获取一部分结果集来进行使用
当需要更多结果集时, 使用查询SQL时创建的这个游标 执行FETCH就可以获取后面的查询结果集.
优点:
降低jdbc客户端的内存使用量.每次只需要返回和接受一部分数据就可以了.
将值修改为 direct之后:
客户端需要全量返回数据, 对客户端内内存要求变高,但是大量数据的查询性能会变好 
并且便于分析问题. 知道是谁被阻塞了. 

2. sendStringParametersAsUnicode 
这个参数是为了解决非utf8字符集情况下 unicode乱码问题的.
他会自动给应用程序生成的SQL做一次varchar类型到 nvarchar类型的转换.
会导致产品中大量出现隐式转换, 引起性能下降. 
微软官方解释, 曾经将这个参数修改成false, 将数据库CPU使用率从100%,优化到30%

但是存在一个风险是,如果代码开发时 数据库时 nvarchar字段,用于存放多国语言, 但是程序里面使用的是varcahr字段类型
有一定的概率出现乱码的情况. 需要应用层进行适配和修改. 

需要注意, 单独修改链接字符串可能无效, 需要jdbc解析字符串,并且准确的进行参数传递. 

其他的优化知识

前期在查看durid连接池时,发现应用层总有一个: PrepateStatement 的参数信息
Oracle数据库很早之前就已经实现了. 

应用层进行了SQL变量绑定之后,理论上每次过来的参数可以实现软软解析,最大程度的降低数据库的CPU算力.提高性能. 

SQLServer数据库这一方面应该也有, 但是需要改天进行一下验证

一个简要总结

修改参数和修改驱动其实没啥太大的区别
建议必须做一下比较完备的功能验证. 

一方面不应该导致性能下降, 至少应该在80%的大规模使用的场景上有性能提升才可以. 
一方面 应该保证已有功能准确可用, 不会出现程序错误和乱码以及其他异常问题

如果仅仅是一两个功能有问题,可以找对应的开发进行修改和完善
如果是大批量的报错, 则需要综合评估一下修改的成本和产出. 

官网的部分参数

第一个: 
sendStringParameters
If the sendStringParametersAsUnicode property is set to "true", String parameters are sent to the server in Unicode format.
If the sendStringParametersAsUnicode property is set to "false", String parameters are sent to the server in non-Unicode format such as ASCII/MBCS instead of Unicode.
The default value for the sendStringParametersAsUnicode property is "true".

第二个: 
selectMethod
If this property is set to "cursor," a database cursor is created for each query created on the connection for TYPE_FORWARD_ONLY and CONCUR_READ_ONLY cursors. 
This property is typically required only if the application generates large result sets that can't be fully contained in client memory. 
If this property is set to "cursor," only a limited number of result set rows are kept in client memory.
The default behavior is that all result set rows are kept in client memory. 
This behavior provides the fastest performance when the application is processing all rows.

第三个:
serverPreparedStatement
(Version 6.2+) 
This property can be used to control how many outstanding prepared statement discard actions (sp_unprepare) can be outstanding per connection 
before a call to clean up the outstanding handles on the server is executed.
If this property is set to <= 1, unprepare actions are executed immediately on prepared statement close. 
If it's set to > 1, these calls are batched together to avoid overhead of calling sp_unprepare too often.

其他类型: 
workstationID
The workstation ID. Used to identify the specific workstation in various profiling and logging tools.

userName
The database user, if connection with SQL user and password.

sendTimeAsDatetime
Set to "true" to send java.sql.Time values to the server as SQL Server datetime values.
Set to "false" to send java.sql.Time values to the server as SQL Server time values.

一个其他的知识

使用mysql的预编译对象PrepateStatement时,
一定需要设置useServerPrepStmts=true开启服务器预编译功能,
设置cachePrepStmts=true开启客户端对预编译对象的缓存。

作者:zhuke
链接:https://www.jianshu.com/p/d46f84aef4bc