How MySQL connector jar version can affect query performance? - java

Currently, in my application with mysql-connector-java:5.1.36, everything works fine. But when I upgrade connector to mysql-connector-java:5.1.47, a query starts to take minutes-2-hours time to execute. If I run the same query directly from the terminal or from the application with v5.1.36, it takes less than a few seconds to execute.
How MySQL connector jar version can affect query performance?

I found the reason. There is a change in mysql-connector-java:5.1.47 and above:
When UTF-8 is used for characterEncoding in the connection string, it maps to the MySQL character set name utf8mb4. While for mysql-connector-java:5.1.46 and below it corresponds to utf8 (or utf8mb3 more appropriately).
Link: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html
For my database character encoding is set to utf8 (or utf8mb3 more appropriately). Due to this index was not being used in query. After setting server property
character_set_server : utf8
it is working fine.

Related

Mysql connector/j 8.0.29 causes incorrect string exception

I am having trouble inserting utf8 string into a mysql database using ‘mysql connector/j ver 8.0.29’
I am working on a java springboot application.
This problem does not happen in ‘mysql connector/j ver 8.0.27’
Anybody encounter this problem?
—-
Mysql server is also 8.0.29 but there is no problem since I can successfully execute ‘INSERT’ sql command using UTF-8 on the server itself
It is only when sending ‘INSERT’ sql command via client pc using springboot that this problem happen
Server pc OS is windows10, client pc is Windows11
This is my table.
This is the error.
Caused by: java.sql.SQLException: Incorrect string value: '\x95|\x82\xA2\x98b' for column 'path' at row 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
this is the connection string:
spring.datasource.url=jdbc:mysql://localhost/${xpac.sql-database-name}?serverTimezone=Asia/Tokyo
this is the error:
Caused by: java.sql.SQLException: Incorrect string value: '\x95|\x82\xA2\x98b' for column 'path' at row 1 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
怖い話 ---> '\x95|\x82\xA2\x98b'
Somewhere in the processing, either cp932 or sjis was used.
'Proof' -- Using either CHARACTER SET cp932 or sjis in
HEX(CONVERT('怖い話' USING ...)) => '957C82A29862'
Based on your comments it seems to be caused by a change in Connector/J
(No, I don't know how to fix it; but this might help in chasing it down.)
Found the cause.
The java application itself when run as service uses the OS default character encoding. Which is obviously not UTF-8. This causes unexpected errors when the application is interacting with a database.
When running the application, it is advisable to explicitly specify that it should use UTF-8. And this is done by specifying the java option Dfile.encoding=utf-8
Perhaps in mysql ver 8.0.27, it forces data to be saved to use UTF-8.
Then when it was updated to mysql ver 8.0.29 this was removed and you have to specify explicitly that you will use UTF-8 encoding.

Cannot read special characters with JDBC

I'm having problem reading UTF-8 data from MySQL database by using MySQL Connector v. 8.0.19. Scandic letters, such as "äö" are replaced with unknown characters. I already made sure the database and its tables and columns are using utf8mb4. Then I added useUnicode=true&characterEncoding=UTF-8 to JDBC connection string, but the outcome is still unexpected. I'm running MySQL CE v. 8 in a Docker container. I can see the scandic letters fine when I run the SELECT queries in a command-line.
I solved this problem by passing --default-character-set=utf8mb4 to MySQL command-line before creating the schema from a separate file. I could add this option to MySQL server configuration as a default.

How do I enforce MySQL to use utf8mb4 using JDBC connector without restarting the server?

My current configuration is as follows:
MySQL server version: 5.7.18
Operating System: Ubuntu 14.04
JDBC Connector: 5.1.42
The problem: I can't use the configuration file in my.cnf since it will require a server restart. Since I'm using MySQL version 5.7 it supports utf8mb4 and I've updated to the latest connector for the version 5.xx series.
I tried using init_connect as given in the documentation here using the following command and using mysql user without super user priviledges:
SET GLOBAL init_connect="SET NAMES 'utf8mb4';SET character_set_server='utf8mb4';";
and setting the connection url as follows:
jdbc:mysql://localhost:3306/xxxx?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&connectionCollation=utf8mb4_unicode_ci
I also tried adding session variable to the URL as follows but it doesn't work.
sessionVariables=character_set_server=utf8mb4,character_set_client=utf8mb4,character_set_connection=utf8mb4,character_set_results=utf8mb4
Can anyone help how to figure the connection settings to allow storing using utf8mb4?
P.S: Utf-8 works fine but it does not work with utf8mb4, so can't insert emoji's.

Jaybird / JDBC + National characters in path

I'm working with Firebird database in Java. Everything works fine, but I have problem with connecting to my database if database file path contains national characters, eg. "á" or "č".
Sample exception:
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544344. I/O error during "CreateFile (open)" operation for file "Z:/testing/á/sample.fdb"
Path is correct, database exists. Jaybird / JDBC has problem with "á" character in path.
Any ideas how to fix it or where is problem? Thanks for all responses.
OS: Windows 7 Pro 64 bit
JDK: 1.7.0.25
Jaybird: 2.2.3
Starting with Jaybird 3, database filename will always be sent as UTF-8, if the server is Firebird 2.5 or higher.
Jaybird 2.2 and earlier has limited support for special characters in the databasename. There are several options you can use to workaround this problem, but if they actually work depends highly on the version of Firebird and the default characterset of the OS (where Firebird is running).
Option 1: use connection property filename_charset=<name of charset> where <name of charset> is the default character set of the operating system where the Firebird server is running.
For example:
jdbc:firebirdsql://myserver/mydatabase?filename_charset=Cp1252
Option 2 (Firebird 2.5 or higher, with Jaybird 2.2): use the workaround described in JDBC-251:
Start your Java application with -Dfile.encoding=UTF8 and include utf8_filename=1 in the connection URL:
jdbc:firebirdsql://myserver/mydatabase?utf8_filename=1
When using this option, make sure that you already specify a connection characterset using connection property charSet, localEncoding or local_encoding (for Java character set names), and/or encoding or lc_ctype (for Firebird character set names). If not, you are using Firebird character set NONE which uses the JVM default character set, and you will need to set the charSet to the 'normal' default encoding of your JVM to prevent character set conversion issues because of the changed value of file.encoding (in some cases - in addition to specifying charSet - you may also explicitly need to set encoding to NONE).
Option 3: Define an alias with only ASCII characters in aliases.conf of the firebird server for the database and connect using this alias instead:
jdbc:firebirdsql://myserver/thealias
Disclosure: I am one of the Jaybird developers

MySQL does not store properly some UTF8 chars

I'm using the mysql dbms to store pages from Wikipedia. I've set the character-set encoding to utf-8 (wikipedia encoding) in my.cnf file with the directive:
[mysqld]
character_set_server = utf8
And created my database with the 'chararacter set utf8' property definition.
I've also changed the charset-encoding for mysqld client by:
inserting the 'charSet=utf8' property when initializing my jdbc driver.
doing a query to 'set names utf8'
However I've noticed that mysql server replaces some characters with others.
For example it replaces á with a.
UPDATE
I've run the command show variables like '%char%' ensuring that both character_set_client and character_set_set are utf8.
How can I store the correct chars in my db? Thanks!
Try to specify the encoding in the DB URL :
url="jdbc:mysql://localhost:port/DBNAME?characterEncoding=UTF-8"
Here's some more information regarding my answer :
The following is taken from the MySQL documentation (http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-charsets.html) :
All strings sent from the JDBC driver to the server are converted
automatically from native Java Unicode form to the client character
encoding, including all queries sent using Statement.execute(),
Statement.executeUpdate(), Statement.executeQuery() as well as all
PreparedStatement and CallableStatement parameters with the exclusion
of parameters set using setBytes(), setBinaryStream(),
setAsciiStream(), setUnicodeStream() and setBlob().
Setting the Character Encoding
The character encoding between client
and server is automatically detected upon connection. You specify the
encoding on the server using the character_set_server for server
versions 4.1.0 and newer. The driver automatically uses the encoding
specified by the server. To override the automatically detected
encoding on the client side, use the characterEncoding property in the
URL used to connect to the server. To allow multiple character sets
to be sent from the client, use the UTF-8 encoding, either by
configuring utf8 as the default server character set, or by
configuring the JDBC driver to use UTF-8 through the characterEncoding
property.
I encountered a similar problem a few months ago. I checked the default value of character_set_server on my MySQL (using the “mysqld --verbose –help” command).
It was latin1.

Categories

Resources