我有Percona Mysql服务器和带有自定义ORM的Java客户端。在数据库中,我有表格:
CREATE TABLE `PlayerSecret` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`created` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
`secret` binary(16) NOT NULL,
`player_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `PlayerSecret_secret_unique` (`secret`),
KEY `PlayerSecret_player_id` (`player_id`)
) ENGINE=InnoDB AUTO_INCREMENT=141 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
我发现该查询SELECT PlayerSecret.player_id FROM PlayerSecret WHERE PlayerSecret.secret = ?
当java.sql.PreparedStatement#setBytes
方法提供参数时,返回空结果集,并通过java.sql.PreparedStatement#setBinaryStream
正常工作。我启用了mysql一般日志,发现该日志中的两个查询都相同,我已在十六进制模式下进行了检查。
一般日志如下:
SELECT PlayerSecret.player_id FROM PlayerSecret WHERE PlayerSecret.secret = '<96>R\Ø8üõA\í¤Z´^E\Ô\ÊÁ\Ö'
以十六进制模式从常规日志中查询参数:
2796 525c d838 fcf5 415c eda4 5ab4 055c d45c cac1 5cd6 27
数据库中的值:
mysql> select hex(secret) from PlayerSecret where id=109;
+----------------------------------+
| hex(secret) |
+----------------------------------+
| 9652D838FCF541EDA45AB405D4CAC1D6 |
+----------------------------------+
1 row in set (0.00 sec)
问题是我的ORM通过setBytes
方法执行此查询,我认为这对于BINARY
数据类型是正确的方法,但它不起作用。
my.cnf
的一部分具有编码设置(可能很重要):
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
general_log = on
general_log_file=/var/log/mysql/mysqld_general.log
require_secure_transport = ON
init-connect = SET collation_connection = utf8mb4_unicode_ci
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
Java代码:
var uuid = UUID.fromString("9652d838-fcf5-41ed-a45a-b405d4cac1d6");
var array = ByteBuffer.allocate(16).putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array();
// works
stmt.setBinaryStream(index, new ByteArrayInputStream(array));
// don't works
stmt.setBytes(index, array);
我无法理解两种情况之间的区别,以及如何解决setBytes
变体的问题。
也许有人可以澄清这一点或将我指向重要的地方/地方?
我的环境:
答案 0 :(得分:0)
最后我弄清楚了。问题出在character_set_client=utf8
而不是utf8mb4
。
该查询显示期望值和实际线程值之间的差异(认为这是非常方便的查询):
SELECT VARIABLE_NAME, gv.VARIABLE_VALUE 'Global', tv.VARIABLE_VALUE 'Thread value', THREAD_ID, PROCESSLIST_ID
FROM performance_schema.global_variables gv
JOIN performance_schema.variables_by_thread tv USING (VARIABLE_NAME)
JOIN performance_schema.threads USING(THREAD_ID)
WHERE gv.VARIABLE_VALUE <> tv.VARIABLE_VALUE ;
+-----------------------+--------------------+--------------------+-----------+----------------+
| VARIABLE_NAME | Global | Thread value | THREAD_ID | PROCESSLIST_ID |
+-----------------------+--------------------+--------------------+-----------+----------------+
| autocommit | ON | OFF | 82 | 56 |
| character_set_client | utf8mb4 | utf8 | 82 | 56 |
| character_set_results | utf8mb4 | | 82 | 56 |
当我在init-connect = SET collation_connection = utf8mb4_unicode_ci
中将init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'
替换为my.cnf
时,问题消失了,尽管setBytes
的查询开始按预期进行。
为什么它对setBinaryStream
有效,而对setBytes
不有效-
因为在第一种情况下,此代码com.mysql.cj.ServerPreparedQueryBindings#setBinaryStream(int, java.io.InputStream, int)
有效:
@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) {
if (x == null) {
setNull(parameterIndex);
} else {
ServerPreparedQueryBindValue binding = getBinding(parameterIndex, true);
this.sendTypesToServer.compareAndSet(false, binding.resetToType(MysqlType.FIELD_TYPE_BLOB, this.numberOfExecutions));
binding.value = x;
binding.isLongData = true;
binding.bindLength = this.useStreamLengthsInPrepStmts.getValue() ? length : -1;
}
}
这里重要的部分是binding.resetToType(MysqlType.FIELD_TYPE_BLOB
-驱动程序注意到mysql该数据是BLOB
在第二种情况下,com.mysql.cj.ServerPreparedQueryBindings#setBytes(int, byte[])
包含:
@Override
public void setBytes(int parameterIndex, byte[] x) {
if (x == null) {
setNull(parameterIndex);
} else {
ServerPreparedQueryBindValue binding = getBinding(parameterIndex, false);
this.sendTypesToServer.compareAndSet(false, binding.resetToType(MysqlType.FIELD_TYPE_VAR_STRING, this.numberOfExecutions));
binding.value = x;
}
}
MysqlType.FIELD_TYPE_VAR_STRING
表示这不是简单的字节,而是采用某种编码(带有某种排序规则)的字符串。
我真的不知道为什么驱动程序将这种类型的数据设置为字节型-这个问题对我来说一直没有解决。