Context & Problem
My company decided to change from the MySQL driver to the MariaDB driver/connector, and still use the MySQL Server DB as the database server for our Spring App.
During this migration, I found problems related to how the driver/connector handles Dates, which results in the following error:
Illegal mix of collations for operation '<='
This occur when running the following query, notice the date comparison on the query's last line.
String sql2 = "select coalesce(sum(b.value), 0) " +
" from acc_sub_account_booking b " +
" join acc_sub_account sa ON sa.id = b.subAccount_id " +
" join km_cash_bond cb ON cb.virtualPayInAccountNumber = sa.iban " +
" join km_rented_object ro ON ro.id = cb.rentedObject_id " +
" where b.bookingType in ('PAY_IN', 'PAY_OUT') " +
" and ro.id = :rentedObjectId " +
" and b.valueDate <= :today";
Object sum1 = entityManager.createNativeQuery(sql).
setParameter("rentedObjectId", pRentedObject.getId()).
setParameter("today", new LocalDateTime(d.getYear(), d.getMonthOfYear(), d.getDayOfMonth(), 23, 59)).
getSingleResult();
Versions:
- Java: 7
- MySQL: 5.5
- Maria Driver: 1.4.6
Understanding
After reading the MySQL documentation, it mentions:
MySQL Connector/J is flexible in the way it handles conversions between MySQL data types and Java data types.
In general, any MySQL data type can be converted to a java.lang.String, and any numeric type can be converted to any of the Java numeric types, although round-off, overflow, or loss of precision may occur.
Perhaps, the error didn't occur before because the MySQL connector handles the conversion between String and Date, and the error now results from trying to compare two different data types.
This error does not occur when using MariaDB as the server, maybe the conversion handling in Maria in done at the database level and not in the connector.
Tests
I believed to be a encoding/character set problem, and changed all the tables and columns to utf8_general_ci. This did not resolve the problem.
I ran some tests - always using the MariaDB Driver when running the query through the application - and got the following results:
Although, tests pass when using MariaDB, this is not an option.
Tests #3 and 4 works, if the query parameters is cast as date:
... and b.valueDate <= DATE(:today)";but this would imply many changes to the code.Test #2 is (the only one that failed and) the option I would like to follow, as it implies the less amount of changes. However, I can not seem to make it work.
? Question ?
Is there a way to use MySQL and the MariaDB connector without causing this problems?
Is there a better option than casting
DATE(:today)all the parameters to date?Another solution? Thank you.
Update:
These are the data source properties set in the code:
dataSource.setJdbcUrl("jdbc:mysql://"+ hostname + ":3306/"
+ databaseName +
"?useUnicode=true&" +
"characterEncoding=utf-8");
Update #2:
More information:
The followings sets were also executed:
ALTER DATABASE km CHARACTER SET utf8 COLLATE utf8_general_ci;
SET collation_connection = 'utf8_general_ci';
SET collation_server = 'utf8_general_ci';
Also, every INFORMATION_SCHEMA.COLUMNS and INFORMATION_SCHEMA.TABLES was CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;.
@elenst solution steps:
- Enabled general_log;
- Confirmed the values for
NAMESandcharacter_set_resultsare the same (latin1 and NULL) when using MySQL connector; - Switched back to MariaDB and set these values
- Run with application/query and the error still persists.
- I also tried with
?sessionVariables=character_set_client=latin1, and?sessionVariables=character_set_client=utf8, and the result was the same.
@DiegoDupin Cannot apply a Timestamp.valueOf() to a Joda Time LocalDateTime. You can wrap it:
setParameter("today", Timestamp.valueOf(String.valueOf(new LocalDateTime(d.getYear(), d.getMonthOfYear(), d.getDayOfMonth(), 23, 59))))but it results in a Timestamp format error.setParameter("today", new LocalDateTime(d.getYear(), d.getMonthOfYear(), d.getDayOfMonth(), 23, 59).toDate())will however work.
The question remains, other parts of the system, due to the driver change, can still share this the fault.
@RickJames: SHOW CREATE TABLE acc_sub_account_booking. The comparison is done on the valueDate field, type date, although the same happens for a datetime field present in another (similar) query.
| acc_sub_account_booking | CREATE TABLE `acc_sub_account_booking` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`creationDate` datetime DEFAULT NULL,
`lastModifiedDate` datetime DEFAULT NULL,
`bookingText` varchar(255) DEFAULT NULL,
`bookingType` varchar(255) DEFAULT NULL,
`uuid` varchar(255) DEFAULT NULL,
`value` decimal(19,2) DEFAULT NULL,
`valueDate` date DEFAULT NULL,
`zkaGVC` int(4) NOT NULL,
`subAccount_id` bigint(20) DEFAULT NULL,
`bankStatementDate` date DEFAULT NULL,
`counterpartHolder` varchar(255) DEFAULT NULL,
`counterpartIban` varchar(255) DEFAULT NULL,
`customerReference` varchar(255) DEFAULT NULL,
`endToEndReference` varchar(255) DEFAULT NULL,
`returnReason` varchar(255) DEFAULT NULL,
`customerSpecificInformations` text,
`counterpartBic` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uuid` (`uuid`),
KEY `FK_SAB_SA` (`subAccount_id`),
CONSTRAINT `FK_SAB_SA` FOREIGN KEY (`subAccount_id`) REFERENCES `acc_sub_account` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3407 DEFAULT CHARSET=utf8 |
Final Update:
The problem listed here can be solved by casting to a Java Date object, instead of directly using JodaTime:
setParameter("today", new LocalDateTime(d.getYear(), d.getMonthOfYear(), d.getDayOfMonth(), 23, 59).toDate())
Nonetheless, other problems were found and in the end, my company decided to revert their decision of using the MariaDB driver.
A very big thank you to all who offer their time to try to help.

valueDate? (OrSHOW CREATE TABLE) Can you display the sql after:todayhas been substituted?buchungszeitpunkt? orb.valueDate??