Bug #109346 Add support for Virtual Threads on the JDK
Submitted: 12 Dec 2022 17:10 Modified: 12 Mar 12:14
Reporter: Bart De Neuter (OCA) Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S4 (Feature request)
Version:8.0 OS:Any
Assigned to: CPU Architecture:Any

[12 Dec 2022 17:10] Bart De Neuter
Description:
Support for virtual threads is being added to the JDK. It has been added as a preview feature to JDK 19. This allows you to run applications on lightweight threads and when IO needs to happen the virtual thread blocks but the underlying carrier thread (OS thread) can perform other work.
There is a limition when code is executed in a synchronized block. In a synchronized block the carrier thread will be pinned and won't be available to perform other work.
These synchronized blocks where IO is being done, should be changed and use a ReentrantLock.

It seems the JDBC driver calls the database server in synchronized blocks.

More information can be found here:
https://openjdk.org/jeps/425

`There are two scenarios in which a virtual thread cannot be unmounted during blocking operations because it is pinned to its carrier:

When it executes code inside a synchronized block or method, or
When it executes a native method or a foreign function.
Pinning does not make an application incorrect, but it might hinder its scalability. If a virtual thread performs a blocking operation such as I/O or BlockingQueue.take() while it is pinned, then its carrier and the underlying OS thread are blocked for the duration of the operation. Frequent pinning for long durations can harm the scalability of an application by capturing carriers.`

How to repeat:
Not a bug
[13 Dec 2022 6:20] MySQL Verification Team
Hello Bart De Neuter,

Thank you for the feature request.

regards,
Umesh
[20 Dec 2022 7:50] Bart De Neuter
I have created a PR to replace synchronized blocks by a ReentrantLock. Please feel free to accept or decline the PR.

https://github.com/mysql/mysql-connector-j/pull/91

Kind regards,
Bart
[18 Jan 2023 11:56] Bart De Neuter
Hi,

we are performing more tests with this patch and I noticed that the connectionMutex in ConnectionImpl can be used in other places then this class.
ConnectionImpl has a `synchronized(connectionMutex)` but the implementations of this proxy have also synchronized methods. So by using only a ReentrantLock in the ConnectionImpl class, we alter the locking behaviour of the Driver. We are fixing this in our patch for testing. We didn't notice any errors with this patch and the tests passed. But it is safer to ignore this patch.
We can always resubmit our current complete patch if needed or we can wait until Virtual Threads supports synchronized and don't alter the code.
[9 Nov 2023 12:30] Paul Carter-Brown
Is there any update or plan to prevent pinning in the MySQL driver. This is a barrier to virtual threads adoption and seems to have been solved on Postgres. I see there was a PR for this mentioned on github but not merged. Thanks
[9 Nov 2023 16:07] Bart De Neuter
This was a first PR when we started testing the MySQL driver with the JDK 19 release.
Afterwards we have created another PR that makes this PR obsolete.
You will find the new PR here:
https://bugs.mysql.com/bug.php?id=110512
[12 Mar 12:14] Filipe Silva
Superseded by Bug#110512.