cannot be cast to class com.mysql.cj.jdbc.ClientPreparedStatement
Posted by: Tomonori Iida
Date: April 12, 2023 09:32AM

Hi
We are developing a multi-threaded batch application on Spring Boot 2.7.8 + Aurora MySQL 2.11.1 (primary-db/replica-db).
I am having trouble with ClassCastException when updating/inserting data into a table.
If I update only one item, ClassCastException is not raised.
Only when updating a large number of n updates, the ClassCastException is always thrown.
Also, when I connect with MariaDB Connector/J 2.5.4 instead of MySQL Connector/J 8.0.32, no ClassCastException occurs even for mass updates.
I would say that using MariaDB Connector/J would solve the problem, but..,

# HikariCP
## MySQL Connector/J
- name: "default"
dataSource:
driverClassName: "com.mysql.cj.jdbc.Driver"
jdbcUrl: "jdbc:mysql:replication://\
primary-db:3306\
,replica-db:3306\
?rewriteBatchedStatements=true&sessionVariables=character_set_connection=utf8mb4,collation_connection=utf8mb4_general_ci&socketTimeout=300000"
username: "root"
password: ""
autoCommit: true
readOnly: false
connectionTimeout: 300000
idleTimeout: 600000
minimumIdle: 2
maximumPoolSize: 10
connectionTestQuery: "SELECT 1"

## MariaDB Connector/J
hikari:
- name: "default"
dataSource:
driverClassName: "org.mariadb.jdbc.Driver"
jdbcUrl: "jdbc:mysql:aurora://\
primary-db:3306\
,replica-db:3306\
?rewriteBatchedStatements=true&sessionVariables=character_set_connection=utf8mb4,collation_connection=utf8mb4_general_ci&socketTimeout=300000"
username: "root"
password: ""
autoCommit: true
readOnly: false
connectionTimeout: 300000
idleTimeout: 600000
minimumIdle: 2
maximumPoolSize: 10
connectionTestQuery: "SELECT 1"

# ClassCastException
2023-xx-xx xx:yy:zz [ForkJoinPool-4-worker-23] ERROR o.h.e.j.b.i.BatchingBatch -> HHH000315: Exception executing batch [java.lang.ClassCastException: class com.sun.proxy.$Proxy186 cannot be cast to class com.mysql.cj.jdbc.ClientPreparedStatement (com.sun.proxy.$Proxy186 and com.mysql.cj.jdbc.ClientPreparedStatement are in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @7823a2f9)], SQL: update test.table1 set updated_at=?, account_id=?, hoge_formats=?, hoge_type=?, body=?, action_type=?, cr_type=?, story_id=?, hash=?, name=?, story_spec=?, object_type=?, status=?, title=?, video_id=? where id=?

# DDL
CREATE TABLE `test`.`table1` (
`id` bigint(20) NOT NULL,
`account_id` varchar(128) NOT NULL,
`hash` varchar(128) DEFAULT NULL,
`video_id` varchar(128) DEFAULT NULL,
`action_type` varchar(128) DEFAULT NULL,
`title` varchar(512) DEFAULT NULL,
`body` mediumtext,
`name` varchar(256) DEFAULT NULL,
`story_id` varchar(192) DEFAULT NULL,
`object_type` varchar(32) DEFAULT NULL,
`cr_type` varchar(32) NOT NULL,
`status` varchar(64) NOT NULL,
`story_spec` json DEFAULT NULL,
`hoge_type` varchar(128) DEFAULT NULL,
`hoge_formats` varchar(255) DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `idx_accid` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

# Entity
@EqualsAndHashCode(callSuper = false)
@Builder
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
@Data
@Entity
@Table(catalog = "test", name = "table1")
public class Table1 extends BaseEntity {
private static final long serialVersionUID = 1L;
@Id
private BigInteger id;
private String accountId;
private String hash;
private String videoId;
@Enumerated(EnumType.STRING)
private ActionType actionType;
private String title;
private String body;
private String name;
private String storyId;
@Enumerated(EnumType.STRING)
private ObjectType objectType;
@Convert(converter = CrTypeToStringConverter.class)
private CrType crType;
@Enumerated(EnumType.STRING)
private Status status;
@Convert(converter = StorySpecToStringConverter.class)
private StorySpec storySpec;
@Enumerated(EnumType.STRING)
private HogeType hogeType;
@Convert(converter = StringsToStringConverter.class)
private List<String> hogeFormats;
}


Please advise if you have any suggestions.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.