fix(jdbc): share embedded H2 runtimes with isolated logical sessions

Closes #10923
This commit is contained in:
t8y2 committed 2026-10-02 06:56:21 +00:00
1 parent 9e52ac60c4
commit b57b3873d9
8 files changed
+1534 -56

No files matched your search

@@ -0,0 +1,428 @@
use super::*;
async fn fixture() -> (AppState, std::path::PathBuf) {
use std::os::unix::fs::PermissionsExt;
let (mut state, directory) = super::tests::test_app_state().await;
let plugins = directory.join("plugins");
let plugin = plugins.join("jdbc");
std::fs::create_dir_all(&plugin).unwrap();
let script = plugin.join("driver.py");
std::fs::write(
&script,
r#"#!/usr/bin/python3
import json, os, sys
sessions = {}
slow_close = set()
with open(os.path.join(os.path.dirname(__file__), 'pids'), 'a') as pids:
pids.write(str(os.getpid()) + '\n')
for line in sys.stdin:
request = json.loads(line)
method, params = request['method'], request.get('params', {})
session = params.get('jdbcSessionId', 'legacy')
result = {'pid': os.getpid(), 'session': session}
response = {'id': request['id']}
if method == 'jdbcSessionProtocol':
result = {'version': 2}
elif method == 'openJdbcSession':
sessions[session] = None
if os.path.exists(os.path.join(os.path.dirname(__file__), 'delay-open')):
open(os.path.join(os.path.dirname(__file__), 'opening'), 'w').close()
continue
elif method == 'connect':
if params['connection'].get('username') == 'fail':
response['error'] = {'message': 'connect failed'}
else:
sessions[session] = params['connection']['connection_string']
if params['connection'].get('username') == 'delay-close':
slow_close.add(session)
elif method == 'closeJdbcSession':
sessions.pop(session, None)
if session in slow_close:
continue
elif method == 'wait':
open(os.path.join(os.path.dirname(__file__), 'waiting'), 'w').close()
continue
elif method == 'exit':
sys.exit(0)
else:
result['url'] = sessions.get(session)
result['session_count'] = len(sessions)
response.setdefault('result', result)
print(json.dumps(response), flush=True)
"#,
)
.unwrap();
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
std::fs::write(plugin.join("manifest.json"), serde_json::json!({
"id": "jdbc", "name": "JDBC", "version": "0.1.41", "protocol_version": 1,
"executable": "driver.py", "drivers": [{"id": "jdbc", "label": "JDBC", "kind": "external", "database_type": "jdbc"}]
}).to_string()).unwrap();
state.plugins = PluginRegistry::new_with_app_version(plugins, "0.6.30");
let mut config = super::tests::mysql_config(None);
config.db_type = DatabaseType::Jdbc;
config.connection_string = Some("jdbc:h2:file:./sample;LOCK_TIMEOUT=2000".into());
config.jdbc_driver_class = Some("org.h2.Driver".into());
state.configs.write().await.insert(config.id.clone(), config);
(state, directory)
}
async fn session(state: &AppState, key: &str) -> Arc<PluginDriverSession> {
match state.pool_handle(key).await.unwrap() {
PoolKind::ExternalDriver { session, .. } => session,
_ => panic!("expected external JDBC driver"),
}
}
async fn identity(session: &PluginDriverSession) -> serde_json::Value {
session.invoke("identity", serde_json::json!({})).await.unwrap()
}
#[tokio::test]
async fn embedded_h2_jdbc_shares_process_but_preserves_pool_and_session_ownership() {
let (state, directory) = fixture().await;
let metadata = state.get_or_create_metadata_pool_for_session("conn", None, None).await.unwrap();
let first_key = state.get_or_create_pool_for_session("conn", None, Some("tab-a")).await.unwrap();
let second_key = state.get_or_create_pool_for_session("conn", None, Some("tab-b")).await.unwrap();
assert_ne!(first_key, second_key);
assert_ne!(metadata, first_key);
let metadata_session = session(&state, &metadata).await;
let first = session(&state, &first_key).await;
let second = session(&state, &second_key).await;
let metadata_identity = identity(&metadata_session).await;
let first_identity = identity(&first).await;
let second_identity = identity(&second).await;
assert_eq!(metadata_identity["pid"], first_identity["pid"]);
assert_eq!(first_identity["pid"], second_identity["pid"]);
assert_ne!(metadata_identity["session"], first_identity["session"]);
assert_ne!(first_identity["session"], second_identity["session"]);
let config = state.configs.read().await["conn"].clone();
state.test_external_driver_with_info("jdbc", &config).await.unwrap();
assert_eq!(std::fs::read_to_string(directory.join("plugins/jdbc/pids")).unwrap().lines().count(), 1);
let mut other = state.configs.read().await["conn"].clone();
other.id = "other".into();
other.connection_string = Some("jdbc:h2:~/other;MODE=PostgreSQL".into());
state.configs.write().await.insert(other.id.clone(), other);
let key = state.get_or_create_pool_for_session("other", None, Some("tab-c")).await.unwrap();
let third = session(&state, &key).await;
let third_identity = identity(&third).await;
assert_eq!(first_identity["pid"], third_identity["pid"]);
assert_ne!(first_identity["session"], third_identity["session"]);
assert_ne!(first_identity["url"], third_identity["url"]);
state.remove_pool_by_key(&first_key).await;
assert!(!first.is_available());
assert_eq!(identity(&second).await["session"], second_identity["session"]);
let reopened_key = state.get_or_create_pool_for_session("conn", None, Some("tab-a")).await.unwrap();
assert_eq!(reopened_key, first_key);
let reopened = identity(session(&state, &reopened_key).await.as_ref()).await;
assert_eq!(reopened["pid"], second_identity["pid"]);
assert_ne!(reopened["session"], first_identity["session"]);
state.shutdown(Duration::from_secs(5)).await;
assert!(metadata_session.pid().await.is_none());
std::fs::remove_dir_all(directory).unwrap();
}
#[tokio::test]
async fn embedded_h2_jdbc_failed_connect_cancel_drop_and_last_close_are_isolated() {
let (state, directory) = fixture().await;
let config = state.configs.read().await["conn"].clone();
let good = state.external_driver_pool("jdbc", &config).await.unwrap();
let PoolKind::ExternalDriver { session: good, .. } = good else { panic!() };
let pid = good.pid().await;
let mut bad = config.clone();
bad.username = "fail".into();
assert!(state.external_driver_pool("jdbc", &bad).await.is_err());
assert_eq!(good.pid().await, pid);
identity(&good).await;
let PoolKind::ExternalDriver { session: waiting, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
let pending_session = waiting.clone();
let pending =
tokio::spawn(async move { pending_session.invoke::<serde_json::Value>("wait", serde_json::json!({})).await });
tokio::time::timeout(Duration::from_secs(5), async {
while !directory.join("plugins/jdbc/waiting").exists() {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
pending.abort();
assert!(pending.await.unwrap_err().is_cancelled());
tokio::time::timeout(Duration::from_secs(5), async {
while waiting.is_available() {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
identity(&good).await;
drop(waiting);
let PoolKind::ExternalDriver { session: dropped, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
drop(dropped);
good.shutdown().await;
tokio::time::timeout(Duration::from_secs(5), async {
while good.pid().await.is_some() {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
let PoolKind::ExternalDriver { session: reopened, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
assert_ne!(reopened.pid().await, pid);
reopened.shutdown().await;
state.shutdown(Duration::from_secs(5)).await;
std::fs::remove_dir_all(directory).unwrap();
}
#[tokio::test]
async fn embedded_h2_jdbc_keeps_remote_memory_and_unrelated_drivers_dedicated() {
let (state, directory) = fixture().await;
let mut config = state.configs.read().await["conn"].clone();
for url in
["jdbc:h2:tcp://localhost/sample", "jdbc:h2:ssl://localhost/sample", "jdbc:h2:mem:test", "jdbc:other:sample"]
{
config.connection_string = Some(url.into());
let PoolKind::ExternalDriver { session: first, .. } =
state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
let PoolKind::ExternalDriver { session: second, .. } =
state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
assert_ne!(first.pid().await, second.pid().await);
assert_eq!(identity(&first).await["session"], "legacy");
first.shutdown().await;
second.shutdown().await;
}
state.shutdown(Duration::from_secs(5)).await;
std::fs::remove_dir_all(directory).unwrap();
}
#[test]
fn embedded_h2_jdbc_url_scope_preserves_native_and_other_vendor_defaults() {
let mut config = super::tests::mysql_config(None);
config.db_type = DatabaseType::Jdbc;
for url in [
"jdbc:h2:file:/data/example",
"jdbc:h2:./example",
"jdbc:h2:~/example;AUTO_SERVER=TRUE",
"jdbc:h2:/data/example",
"jdbc:h2:zip:/data/a.zip!/example",
] {
config.connection_string = Some(url.into());
assert!(is_embedded_h2_jdbc(&config), "{url}");
}
for url in [
"jdbc:h2:tcp://host/db",
"jdbc:h2:ssl://host/db",
"jdbc:h2:mem:",
"jdbc:h2:mem:named",
"jdbc:h2:",
"jdbc:postgresql://host/h2",
] {
config.connection_string = Some(url.into());
assert!(!is_embedded_h2_jdbc(&config), "{url}");
}
config.connection_string = Some("jdbc:h2:file:./example".into());
config.db_type = DatabaseType::H2;
assert!(!is_embedded_h2_jdbc(&config));
}
#[tokio::test]
async fn embedded_h2_jdbc_rejects_old_plugins_and_preserves_selected_driver_runtime() {
let (state, directory) = fixture().await;
let config = state.configs.read().await["conn"].clone();
let script = directory.join("plugins/jdbc/driver.py");
let original = std::fs::read_to_string(&script).unwrap();
std::fs::write(&script, original.replace("'version': 2", "'version': 1")).unwrap();
let error = state.external_driver_pool("jdbc", &config).await.err().unwrap();
assert!(error.contains("Update the JDBC plugin"));
let pid = std::fs::read_to_string(directory.join("plugins/jdbc/pids")).unwrap();
assert!(!std::process::Command::new("kill")
.args(["-0", pid.trim()])
.stderr(std::process::Stdio::null())
.status()
.unwrap()
.success());
std::fs::write(&script, original).unwrap();
let (first, second) =
tokio::join!(state.external_driver_pool("jdbc", &config), state.external_driver_pool("jdbc", &config));
let PoolKind::ExternalDriver { session: first, .. } = first.unwrap() else { panic!() };
let PoolKind::ExternalDriver { session: second, .. } = second.unwrap() else { panic!() };
assert_eq!(first.pid().await, second.pid().await);
let mut different_driver = config.clone();
different_driver.jdbc_driver_paths = vec!["other-selected-version.jar".into()];
let PoolKind::ExternalDriver { session: different, .. } =
state.external_driver_pool("jdbc", &different_driver).await.unwrap()
else {
panic!()
};
assert_ne!(first.pid().await, different.pid().await);
different.shutdown().await;
first.shutdown().await;
second.shutdown().await;
state.shutdown(Duration::from_secs(5)).await;
std::fs::remove_dir_all(directory).unwrap();
}
#[tokio::test]
async fn embedded_h2_jdbc_last_owner_close_can_race_a_new_session() {
let (state, directory) = fixture().await;
let config = state.configs.read().await["conn"].clone();
for cycle_index in 0..8 {
let PoolKind::ExternalDriver { session: closing, .. } =
state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
let (_, replacement) = tokio::time::timeout(Duration::from_secs(5), async {
tokio::join!(closing.shutdown(), state.external_driver_pool("jdbc", &config))
})
.await
.unwrap();
let PoolKind::ExternalDriver { session: replacement, .. } = replacement.unwrap() else { panic!() };
assert!(replacement.is_available(), "cycle {cycle_index}");
identity(&replacement).await;
replacement.shutdown().await;
assert!(replacement.pid().await.is_none());
}
state.shutdown(Duration::from_secs(5)).await;
std::fs::remove_dir_all(directory).unwrap();
}
#[tokio::test]
async fn embedded_h2_jdbc_cancelled_open_releases_its_member_and_server_state() {
let (state, directory) = fixture().await;
let config = state.configs.read().await["conn"].clone();
let PoolKind::ExternalDriver { session: survivor, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
std::fs::write(directory.join("plugins/jdbc/delay-open"), "").unwrap();
{
let opening = state.external_driver_pool("jdbc", &config);
tokio::pin!(opening);
tokio::time::timeout(Duration::from_secs(5), async {
loop {
tokio::select! {
result = &mut opening => panic!("opening unexpectedly finished: {}", result.is_ok()),
_ = tokio::task::yield_now() => {
if directory.join("plugins/jdbc/opening").exists() { break; }
}
}
}
})
.await
.unwrap();
}
tokio::time::timeout(Duration::from_secs(5), async {
while identity(&survivor).await["session_count"] != 1 {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
survivor.shutdown().await;
assert!(survivor.pid().await.is_none());
state.shutdown(Duration::from_secs(5)).await;
std::fs::remove_dir_all(directory).unwrap();
}
#[tokio::test]
async fn embedded_h2_jdbc_replaces_dead_runtime_with_sibling_handles_still_alive() {
let (state, directory) = fixture().await;
let config = state.configs.read().await["conn"].clone();
let PoolKind::ExternalDriver { session: first, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
let PoolKind::ExternalDriver { session: sibling, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
let original_pid = first.pid().await;
assert!(first.invoke::<serde_json::Value>("exit", serde_json::json!({})).await.is_err());
tokio::time::timeout(Duration::from_secs(5), async {
while first.is_available() || sibling.is_available() {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
let PoolKind::ExternalDriver { session: replacement, .. } =
state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
assert_ne!(replacement.pid().await, original_pid);
assert!(replacement.pid().await.is_some());
tokio::join!(first.shutdown(), sibling.shutdown());
identity(&replacement).await;
replacement.shutdown().await;
assert!(replacement.pid().await.is_none());
state.shutdown(Duration::from_secs(5)).await;
std::fs::remove_dir_all(directory).unwrap();
}
#[tokio::test]
async fn embedded_h2_jdbc_timeout_and_interrupted_shutdown_finish_session_cleanup() {
let (state, directory) = fixture().await;
let config = state.configs.read().await["conn"].clone();
let PoolKind::ExternalDriver { session: survivor, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
let PoolKind::ExternalDriver { session: timed, .. } = state.external_driver_pool("jdbc", &config).await.unwrap()
else {
panic!()
};
let error = timed
.invoke_with_timeout::<serde_json::Value>("wait", serde_json::json!({}), Some(Duration::from_millis(20)))
.await
.unwrap_err();
assert!(error.contains("timed out"));
assert!(!timed.is_available());
timed.shutdown().await;
identity(&survivor).await;
let mut delayed = config.clone();
delayed.username = "delay-close".into();
let PoolKind::ExternalDriver { session: closing, .. } = state.external_driver_pool("jdbc", &delayed).await.unwrap()
else {
panic!()
};
let close_session = closing.clone();
let close = tokio::spawn(async move {
close_session.shutdown().await;
});
tokio::time::timeout(Duration::from_secs(5), async {
while closing.is_available() {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
close.abort();
assert!(close.await.unwrap_err().is_cancelled());
identity(&survivor).await;
survivor.shutdown().await;
tokio::time::timeout(Duration::from_secs(5), async {
while survivor.pid().await.is_some() {
tokio::task::yield_now().await;
}
})
.await
.unwrap();
closing.shutdown().await;
state.shutdown(Duration::from_secs(5)).await;
std::fs::remove_dir_all(directory).unwrap();
}
+48 -4
View File
@@ -152,6 +152,7 @@ impl PoolKind {
fn is_available_for_routing(&self) -> bool { fn is_available_for_routing(&self) -> bool {
match self { match self {
Self::Agent(client) => client.is_runtime_available(), Self::Agent(client) => client.is_runtime_available(),
Self::ExternalDriver { session, .. } => session.is_available(),
_ => true, _ => true,
} }
} }
@@ -1860,6 +1861,23 @@ impl AppState {
config: &ConnectionConfig, config: &ConnectionConfig,
) -> Result<ConnectionTestResult, String> { ) -> Result<ConnectionTestResult, String> {
let params = serde_json::json!({ "connection": config }); let params = serde_json::json!({ "connection": config });
if driver_id == "jdbc" && is_embedded_h2_jdbc(config) {
let PoolKind::ExternalDriver { session, .. } = self.external_driver_pool(driver_id, config).await? else {
unreachable!();
};
let result = session
.invoke_with_timeout::<serde_json::Value>(
"testConnection",
params,
Some(external_driver_connect_timeout(config)),
)
.await;
session.shutdown().await;
return result.map(|response| {
ConnectionTestResult::success("Connection successful")
.with_database_info(database_info_from_protocol_value(&response))
});
}
let env = self.external_driver_runtime_env(driver_id)?; let env = self.external_driver_runtime_env(driver_id)?;
let response = self let response = self
.plugins .plugins
@@ -1877,7 +1895,17 @@ impl AppState {
pub async fn external_driver_pool(&self, driver_id: &str, config: &ConnectionConfig) -> Result<PoolKind, String> { pub async fn external_driver_pool(&self, driver_id: &str, config: &ConnectionConfig) -> Result<PoolKind, String> {
let env = self.external_driver_runtime_env(driver_id)?; let env = self.external_driver_runtime_env(driver_id)?;
let session = self.plugins.start_driver_session_for_connection(driver_id, env, &config.name).await?; let session = if driver_id == "jdbc" && is_embedded_h2_jdbc(config) {
let runtime_key = serde_json::to_string(&(
&config.jdbc_driver_class,
&config.jdbc_driver_paths,
&config.agent_java_options,
))
.map_err(|error| error.to_string())?;
self.plugins.start_shared_jdbc_session_for_connection(&runtime_key, env, &config.name).await?
} else {
self.plugins.start_driver_session_for_connection(driver_id, env, &config.name).await?
};
let params = serde_json::json!({ "connection": config }); let params = serde_json::json!({ "connection": config });
let result = session let result = session
.invoke_with_timeout::<serde_json::Value>("connect", params, Some(external_driver_connect_timeout(config))) .invoke_with_timeout::<serde_json::Value>("connect", params, Some(external_driver_connect_timeout(config)))
@@ -4515,9 +4543,9 @@ impl AppState {
} }
} }
PoolKind::PluginConnection(handle) => !handle.is_running(), PoolKind::PluginConnection(handle) => !handle.is_running(),
PoolKind::ExternalDriver { session, .. } => !session.is_available(),
PoolKind::Sqlite(_) PoolKind::Sqlite(_)
| PoolKind::DuckDbWorker(_) | PoolKind::DuckDbWorker(_)
| PoolKind::ExternalDriver { .. }
| PoolKind::MessageQueue | PoolKind::MessageQueue
| PoolKind::Nacos | PoolKind::Nacos
| PoolKind::Consul(_) => false, | PoolKind::Consul(_) => false,
@@ -6432,6 +6460,22 @@ fn normalize_client_session_id(client_session_id: Option<&str>) -> Option<String
client_session_id.map(str::trim).filter(|session| !session.is_empty()).map(|session| session.replace(':', "_")) client_session_id.map(str::trim).filter(|session| !session.is_empty()).map(|session| session.replace(':', "_"))
} }
fn is_embedded_h2_jdbc(config: &ConnectionConfig) -> bool {
if config.db_type != DatabaseType::Jdbc {
return false;
}
let Some(url) = config.connection_string.as_deref() else { return false };
let normalized = url.trim().to_ascii_lowercase();
let Some(location) = normalized.strip_prefix("jdbc:h2:") else { return false };
!location.starts_with("tcp:")
&& !location.starts_with("ssl:")
&& !location.starts_with("mem:")
&& !location.is_empty()
}
#[cfg(all(test, unix))]
mod h2_jdbc_tests;
pub fn task_client_session_id(task_kind: &str, task_id: &str) -> String { pub fn task_client_session_id(task_kind: &str, task_id: &str) -> String {
format!("{task_kind}:{task_id}") format!("{task_kind}:{task_id}")
} }
@@ -7118,7 +7162,7 @@ mod tests {
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
fn mysql_config(database: Option<&str>) -> ConnectionConfig { pub(super) fn mysql_config(database: Option<&str>) -> ConnectionConfig {
ConnectionConfig { ConnectionConfig {
oracle_oci_nls_lang: None, oracle_oci_nls_lang: None,
oracle_oci_tns_admin: None, oracle_oci_tns_admin: None,
@@ -8019,7 +8063,7 @@ mod tests {
assert_eq!(params["connection_string"], "jdbc:sap://hana.example.com:30013/?databaseName=TENANT1&encrypt=true"); assert_eq!(params["connection_string"], "jdbc:sap://hana.example.com:30013/?databaseName=TENANT1&encrypt=true");
} }
async fn test_app_state() -> (AppState, std::path::PathBuf) { pub(super) async fn test_app_state() -> (AppState, std::path::PathBuf) {
let dir = std::env::temp_dir().join(format!("dbx-core-test-{}", uuid::Uuid::new_v4())); let dir = std::env::temp_dir().join(format!("dbx-core-test-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap(); std::fs::create_dir_all(&dir).unwrap();
let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap(); let storage = crate::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
+30 -3
View File
@@ -10,6 +10,7 @@ mod assets;
mod filesystem; mod filesystem;
mod host; mod host;
mod installer; mod installer;
mod jdbc_sessions;
mod lifecycle; mod lifecycle;
mod manifest; mod manifest;
mod marketplace; mod marketplace;
@@ -157,6 +158,7 @@ pub struct PluginRegistry {
root_dir: PathBuf, root_dir: PathBuf,
app_version: String, app_version: String,
lifecycle: PluginLifecycle, lifecycle: PluginLifecycle,
jdbc_runtimes: jdbc_sessions::JdbcRuntimeCache,
} }
impl PluginRegistry { impl PluginRegistry {
@@ -165,7 +167,12 @@ impl PluginRegistry {
} }
pub fn new_with_app_version(root_dir: PathBuf, app_version: impl Into<String>) -> Self { pub fn new_with_app_version(root_dir: PathBuf, app_version: impl Into<String>) -> Self {
Self { root_dir, app_version: app_version.into(), lifecycle: PluginLifecycle::default() } Self {
root_dir,
app_version: app_version.into(),
lifecycle: PluginLifecycle::default(),
jdbc_runtimes: Default::default(),
}
} }
pub fn lifecycle(&self) -> PluginLifecycle { pub fn lifecycle(&self) -> PluginLifecycle {
@@ -381,7 +388,8 @@ fn ensure_plugin_compatible(plugin: &InstalledPlugin) -> Result<(), String> {
pub struct PluginDriverSession { pub struct PluginDriverSession {
sidecar: Arc<PluginSidecarSession>, sidecar: Arc<PluginSidecarSession>,
driver_id: String, driver_id: String,
_activity: lifecycle::PluginUsageGuard, _activity: Option<lifecycle::PluginUsageGuard>,
logical: Option<Arc<jdbc_sessions::JdbcLogicalSession>>,
} }
impl PluginDriverSession { impl PluginDriverSession {
@@ -393,7 +401,7 @@ impl PluginDriverSession {
activity: lifecycle::PluginUsageGuard, activity: lifecycle::PluginUsageGuard,
) -> Result<Self, String> { ) -> Result<Self, String> {
let sidecar = PluginSidecarSession::start(plugin, app_version, env).await?; let sidecar = PluginSidecarSession::start(plugin, app_version, env).await?;
Ok(Self { sidecar, driver_id, _activity: activity }) Ok(Self { sidecar, driver_id, _activity: Some(activity), logical: None })
} }
pub async fn invoke<T>(&self, method: &str, params: serde_json::Value) -> Result<T, String> pub async fn invoke<T>(&self, method: &str, params: serde_json::Value) -> Result<T, String>
@@ -412,13 +420,24 @@ impl PluginDriverSession {
where where
T: DeserializeOwned, T: DeserializeOwned,
{ {
if let Some(logical) = &self.logical {
return logical.invoke(method, params, timeout_duration).await;
}
self.sidecar.invoke_with_timeout(method, params, Some(&self.driver_id), timeout_duration).await self.sidecar.invoke_with_timeout(method, params, Some(&self.driver_id), timeout_duration).await
} }
pub async fn shutdown(&self) { pub async fn shutdown(&self) {
if let Some(logical) = &self.logical {
logical.close().await;
return;
}
self.sidecar.shutdown().await; self.sidecar.shutdown().await;
} }
pub fn is_available(&self) -> bool {
self.logical.as_ref().is_none_or(|logical| logical.is_available())
}
pub async fn pid(&self) -> Option<u32> { pub async fn pid(&self) -> Option<u32> {
self.sidecar.pid().await self.sidecar.pid().await
} }
@@ -446,6 +465,14 @@ impl PluginDriverSession {
} }
} }
impl Drop for PluginDriverSession {
fn drop(&mut self) {
if let Some(logical) = &self.logical {
logical.close_in_background();
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@@ -0,0 +1,182 @@
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak};
use std::time::Duration;
use tokio::sync::Mutex;
use super::{PluginDriverSession, PluginRegistry, PluginRuntimeEnv};
#[derive(Clone, Default)]
pub(super) struct JdbcRuntimeCache(Arc<Mutex<HashMap<String, Weak<JdbcRuntime>>>>);
impl std::fmt::Debug for JdbcRuntimeCache {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("JdbcRuntimeCache")
}
}
struct JdbcRuntime {
process: Arc<PluginDriverSession>,
members: Mutex<(usize, bool)>,
}
pub(super) struct JdbcLogicalSession {
runtime: Arc<JdbcRuntime>,
id: String,
closed: AtomicBool,
operation: Mutex<()>,
close_done: tokio::sync::watch::Sender<bool>,
}
impl PluginRegistry {
pub async fn start_shared_jdbc_session_for_connection(
&self,
runtime_key: &str,
env: PluginRuntimeEnv,
connection_label: &str,
) -> Result<Arc<PluginDriverSession>, String> {
let key = serde_json::to_string(&(runtime_key, &env.vars)).map_err(|error| error.to_string())?;
let mut cache = self.jdbc_runtimes.0.lock().await;
cache.retain(|_, runtime| runtime.strong_count() > 0);
let mut runtime = cache.get(&key).and_then(Weak::upgrade);
if let Some(existing) = &runtime {
let mut members = existing.members.lock().await;
if !members.1 && existing.process.sidecar.status().state == super::PluginSessionState::Running {
members.0 += 1;
} else {
existing.process.sidecar.shutdown().await;
members.1 = true;
drop(members);
runtime = None;
}
}
let runtime = match runtime {
Some(runtime) => runtime,
None => {
let process = self.start_driver_session_for_connection("jdbc", env, connection_label).await?;
let mut startup = StartupGuard(Some(process.clone()));
let protocol = process.invoke::<serde_json::Value>("jdbcSessionProtocol", serde_json::json!({})).await;
if !matches!(protocol, Ok(ref value) if value["version"] == 2) {
process.shutdown().await;
startup.0 = None;
return Err("Update the JDBC plugin to a version supporting embedded H2 logical sessions".into());
}
startup.0 = None;
let runtime = Arc::new(JdbcRuntime { process, members: Mutex::new((1, false)) });
cache.insert(key, Arc::downgrade(&runtime));
runtime
}
};
let logical = Arc::new(JdbcLogicalSession {
runtime: runtime.clone(),
id: uuid::Uuid::new_v4().to_string(),
closed: AtomicBool::new(false),
operation: Mutex::new(()),
close_done: tokio::sync::watch::channel(false).0,
});
let opened: Result<serde_json::Value, _> =
logical.invoke("openJdbcSession", serde_json::json!({}), Some(Duration::from_secs(3))).await;
if let Err(error) = opened {
logical.close().await;
return Err(error);
}
Ok(Arc::new(PluginDriverSession {
sidecar: runtime.process.sidecar.clone(),
driver_id: "jdbc".into(),
_activity: None,
logical: Some(logical),
}))
}
}
impl JdbcLogicalSession {
pub(super) fn is_available(&self) -> bool {
!self.closed.load(Ordering::Acquire)
&& self.runtime.process.sidecar.status().state == super::PluginSessionState::Running
}
pub(super) async fn invoke<T: serde::de::DeserializeOwned>(
self: &Arc<Self>,
method: &str,
mut params: serde_json::Value,
timeout: Option<Duration>,
) -> Result<T, String> {
let _operation = self.operation.lock().await;
if !self.is_available() {
return Err("JDBC logical session is closed; reconnect this session".into());
}
params
.as_object_mut()
.ok_or("JDBC session parameters must be an object")?
.insert("jdbcSessionId".into(), self.id.clone().into());
let mut guard = CancelOnDrop(Some(self.clone()));
let request = self.runtime.process.sidecar.invoke_with_timeout(method, params, Some("jdbc"), None);
let result = match timeout {
Some(duration) => tokio::time::timeout(duration, request)
.await
.map_err(|_| "JDBC logical session request timed out".to_string())?,
None => request.await,
};
guard.0 = None;
result
}
pub(super) async fn close(self: &Arc<Self>) {
self.close_in_background();
let _ = self.close_done.subscribe().wait_for(|done| *done).await;
}
pub(super) fn close_in_background(self: &Arc<Self>) {
if let Ok(handle) = tokio::runtime::Handle::try_current() {
if self.closed.swap(true, Ordering::AcqRel) {
return;
}
let session = self.clone();
handle.spawn(async move {
let runtime = &session.runtime;
let _: Result<serde_json::Value, _> = runtime
.process
.sidecar
.invoke_with_timeout(
"closeJdbcSession",
serde_json::json!({"jdbcSessionId": session.id}),
Some("jdbc"),
Some(Duration::from_secs(3)),
)
.await;
let mut members = runtime.members.lock().await;
members.0 -= 1;
if members.0 == 0 {
members.1 = true;
runtime.process.sidecar.shutdown().await;
}
session.close_done.send_replace(true);
});
}
}
}
struct CancelOnDrop(Option<Arc<JdbcLogicalSession>>);
struct StartupGuard(Option<Arc<PluginDriverSession>>);
impl Drop for StartupGuard {
fn drop(&mut self) {
if let Some(process) = self.0.take() {
if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.spawn(async move {
process.sidecar.shutdown().await;
});
}
}
}
}
impl Drop for CancelOnDrop {
fn drop(&mut self) {
if let Some(session) = &self.0 {
session.close_in_background();
}
}
}
@@ -166,11 +166,28 @@ public final class DbxJdbcPlugin {
new JdbcDriverQuirkRule("jdbc:taos-rs:", TAOS_QUIRKS) new JdbcDriverQuirkRule("jdbc:taos-rs:", TAOS_QUIRKS)
); );
private static String registeredDriverKey = ""; private static String registeredDriverKey = "";
private static String logicalDriverKey;
private static Driver registeredDriver; private static Driver registeredDriver;
private static String sharedConnectionKey = ""; private static ClassLoader registeredDriverClassLoader;
private static Connection sharedConnection; private static final JdbcConnectionState DEFAULT_CONNECTION_STATE = new JdbcConnectionState();
private static boolean manualTransactionActive; private static final ThreadLocal<JdbcConnectionState> CONNECTION_STATE =
private static final Map<String, QuerySession> QUERY_SESSIONS = new HashMap<>(); ThreadLocal.withInitial(() -> DEFAULT_CONNECTION_STATE);
private static final Map<String, JdbcConnectionState> LOGICAL_SESSIONS = new java.util.concurrent.ConcurrentHashMap<>();
private static volatile boolean shuttingDown;
private static final class JdbcConnectionState {
String sharedConnectionKey = "";
Connection sharedConnection;
boolean manualTransactionActive;
final Map<String, QuerySession> querySessions = new HashMap<>();
volatile Statement activeStatement;
volatile boolean closing;
final java.util.concurrent.locks.ReentrantLock operation = new java.util.concurrent.locks.ReentrantLock();
}
private static JdbcConnectionState connectionState() {
return CONNECTION_STATE.get();
}
record JdbcDriverQuirks( record JdbcDriverQuirks(
boolean skipExecutionContext, boolean skipExecutionContext,
@@ -266,6 +283,7 @@ public final class DbxJdbcPlugin {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
java.util.concurrent.ExecutorService requests = java.util.concurrent.Executors.newCachedThreadPool();
try ( try (
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8)) BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8))
@@ -275,21 +293,127 @@ public final class DbxJdbcPlugin {
if (line.isBlank()) { if (line.isBlank()) {
continue; continue;
} }
JsonNode incoming = MAPPER.readTree(line);
if (incoming.path("params").hasNonNull("jdbcSessionId")) {
JdbcConnectionState retainedState = retainLogicalState(incoming);
requests.execute(() -> {
try {
writeResponse(writer, handleRequest(incoming, retainedState));
} catch (Exception error) {
System.err.println("JDBC session response failed: " + error.getClass().getSimpleName());
}
});
continue;
}
if ("close".equals(incoming.path("method").asText())) shuttingDown = true;
ObjectNode response = handleLine(line); ObjectNode response = handleLine(line);
writer.write(MAPPER.writeValueAsString(response)); writeResponse(writer, response);
writer.newLine();
writer.flush();
if (response.path("_dbx_close").asBoolean(false)) { if (response.path("_dbx_close").asBoolean(false)) {
break; break;
} }
} }
} finally { } finally {
shuttingDown = true;
for (JdbcConnectionState state : LOGICAL_SESSIONS.values()) {
closeLogicalSession(state);
}
LOGICAL_SESSIONS.clear();
requests.shutdown();
closeSharedConnection(); closeSharedConnection();
} }
} }
private static void writeResponse(BufferedWriter writer, ObjectNode response) throws IOException {
synchronized (writer) {
writer.write(MAPPER.writeValueAsString(response));
writer.newLine();
writer.flush();
}
}
private static void closeLogicalSession(JdbcConnectionState state) {
state.closing = true;
boolean locked = false;
while (!locked) {
Statement statement = state.activeStatement;
if (statement != null) {
try {
statement.cancel();
} catch (SQLException ignored) {
}
}
try {
locked = state.operation.tryLock(50, java.util.concurrent.TimeUnit.MILLISECONDS);
} catch (InterruptedException ignored) {
}
}
try {
JdbcConnectionState previous = connectionState();
CONNECTION_STATE.set(state);
try {
closeSharedConnection();
} finally {
CONNECTION_STATE.set(previous);
}
} finally {
state.operation.unlock();
}
}
private static JdbcConnectionState retainLogicalState(JsonNode request) {
JsonNode params = request.path("params");
if (!params.hasNonNull("jdbcSessionId") || shuttingDown) return null;
String sessionId = params.path("jdbcSessionId").asText();
String method = request.path("method").asText();
if ("openJdbcSession".equals(method)) {
return LOGICAL_SESSIONS.computeIfAbsent(sessionId, ignored -> new JdbcConnectionState());
}
if ("closeJdbcSession".equals(method)) {
JdbcConnectionState state = LOGICAL_SESSIONS.remove(sessionId);
if (state != null) state.closing = true;
return state;
}
return LOGICAL_SESSIONS.get(sessionId);
}
private static JsonNode handleLogicalSession(String method, JsonNode params, JsonNode connection,
JdbcConnectionState state) throws Exception {
String id = requireText(params, "jdbcSessionId");
if ("closeJdbcSession".equals(method)) {
if (state != null) closeLogicalSession(state);
return okResult();
}
if (state == null) throw new SQLException("Unknown JDBC logical session");
state.operation.lock();
try {
if (shuttingDown || state.closing) throw new SQLException("JDBC logical session is closed");
if ("openJdbcSession".equals(method)) return okResult();
CONNECTION_STATE.set(state);
try {
registerDrivers(connection);
return handle(method, params, connection);
} catch (Exception | LinkageError error) {
if ("connect".equals(method)) {
closeSharedConnection();
state.closing = true;
LOGICAL_SESSIONS.remove(id, state);
}
throw error;
} finally {
state.activeStatement = null;
CONNECTION_STATE.remove();
}
} finally {
state.operation.unlock();
}
}
private static ObjectNode handleLine(String line) throws Exception { private static ObjectNode handleLine(String line) throws Exception {
JsonNode request = MAPPER.readTree(line); JsonNode request = MAPPER.readTree(line);
return handleRequest(request, retainLogicalState(request));
}
private static ObjectNode handleRequest(JsonNode request, JdbcConnectionState retainedState) throws Exception {
JsonNode id = request.path("id"); JsonNode id = request.path("id");
ObjectNode response = MAPPER.createObjectNode(); ObjectNode response = MAPPER.createObjectNode();
response.set("id", id.isMissingNode() ? MAPPER.getNodeFactory().numberNode(1) : id); response.set("id", id.isMissingNode() ? MAPPER.getNodeFactory().numberNode(1) : id);
@@ -299,7 +423,19 @@ public final class DbxJdbcPlugin {
String method = requireText(request, "method"); String method = requireText(request, "method");
JsonNode params = request.path("params"); JsonNode params = request.path("params");
connection = params.path("connection"); connection = params.path("connection");
if ("jdbcSessionProtocol".equals(method)) {
response.set("result", MAPPER.createObjectNode().put("version", 2));
return response;
}
if (params.hasNonNull("jdbcSessionId")) {
response.set("result", handleLogicalSession(method, params, connection, retainedState));
return response;
}
if ("close".equals(method)) { if ("close".equals(method)) {
for (JdbcConnectionState state : LOGICAL_SESSIONS.values()) {
closeLogicalSession(state);
}
LOGICAL_SESSIONS.clear();
closeSharedConnection(); closeSharedConnection();
ObjectNode result = MAPPER.createObjectNode(); ObjectNode result = MAPPER.createObjectNode();
result.put("ok", true); result.put("ok", true);
@@ -590,9 +726,16 @@ public final class DbxJdbcPlugin {
T get() throws SQLException; T get() throws SQLException;
} }
private static void registerDrivers(JsonNode connection) throws Exception { private static synchronized void registerDrivers(JsonNode connection) throws Exception {
String driverKey = driverKey(connection); String driverKey = driverKey(connection);
if (logicalDriverKey != null && !logicalDriverKey.equals(driverKey)) {
throw new SQLException("JDBC logical sessions require the same selected driver");
}
if (connectionState() != DEFAULT_CONNECTION_STATE) logicalDriverKey = driverKey;
if (driverKey.equals(registeredDriverKey) && registeredDriver != null) { if (driverKey.equals(registeredDriverKey) && registeredDriver != null) {
if (registeredDriverClassLoader != null) {
Thread.currentThread().setContextClassLoader(registeredDriverClassLoader);
}
return; return;
} }
closeSharedConnection(); closeSharedConnection();
@@ -612,6 +755,7 @@ public final class DbxJdbcPlugin {
? Thread.currentThread().getContextClassLoader() ? Thread.currentThread().getContextClassLoader()
: new URLClassLoader(urls.toArray(URL[]::new), DbxJdbcPlugin.class.getClassLoader()); : new URLClassLoader(urls.toArray(URL[]::new), DbxJdbcPlugin.class.getClassLoader());
Thread.currentThread().setContextClassLoader(loader); Thread.currentThread().setContextClassLoader(loader);
registeredDriverClassLoader = loader;
String driverClass = optionalText(connection, "jdbc_driver_class"); String driverClass = optionalText(connection, "jdbc_driver_class");
if (driverClass != null) { if (driverClass != null) {
@@ -647,9 +791,10 @@ public final class DbxJdbcPlugin {
throw new IllegalArgumentException("JDBC URL is required."); throw new IllegalArgumentException("JDBC URL is required.");
} }
String key = connectionKey(connection); String key = connectionKey(connection);
if (sharedConnection != null && key.equals(sharedConnectionKey) && !isConnectionClosed(sharedConnection)) { JdbcConnectionState state = connectionState();
configureOrdinaryAutoCommit(sharedConnection); if (state.sharedConnection != null && key.equals(state.sharedConnectionKey) && !isConnectionClosed(state.sharedConnection)) {
return sharedConnection; configureOrdinaryAutoCommit(state.sharedConnection);
return state.sharedConnection;
} }
closeSharedConnection(); closeSharedConnection();
@@ -679,10 +824,10 @@ public final class DbxJdbcPlugin {
// Prefer the explicitly registered driver. DriverManager.getConnection only catches // Prefer the explicitly registered driver. DriverManager.getConnection only catches
// SQLException; Hive/Inceptor drivers may throw UnsupportedOperationException for optional // SQLException; Hive/Inceptor drivers may throw UnsupportedOperationException for optional
// methods, which aborts connect before the intended driver is reached. // methods, which aborts connect before the intended driver is reached.
sharedConnection = connectWithRegisteredDriver(url, properties); state.sharedConnection = connectWithRegisteredDriver(url, properties);
sharedConnectionKey = key; state.sharedConnectionKey = key;
configureOrdinaryAutoCommit(sharedConnection); configureOrdinaryAutoCommit(state.sharedConnection);
return sharedConnection; return state.sharedConnection;
} }
private static Connection connectWithRegisteredDriver(String url, Properties properties) throws SQLException { private static Connection connectWithRegisteredDriver(String url, Properties properties) throws SQLException {
@@ -732,14 +877,14 @@ public final class DbxJdbcPlugin {
} }
private static void configureOrdinaryAutoCommit(Connection jdbcConnection) throws SQLException { private static void configureOrdinaryAutoCommit(Connection jdbcConnection) throws SQLException {
if (manualTransactionActive || hasActiveQuerySession(jdbcConnection) || jdbcConnection.getAutoCommit()) { if (connectionState().manualTransactionActive || hasActiveQuerySession(jdbcConnection) || jdbcConnection.getAutoCommit()) {
return; return;
} }
jdbcConnection.setAutoCommit(true); jdbcConnection.setAutoCommit(true);
} }
private static boolean hasActiveQuerySession(Connection jdbcConnection) { private static boolean hasActiveQuerySession(Connection jdbcConnection) {
return QUERY_SESSIONS.values().stream().anyMatch(session -> session.connection == jdbcConnection); return connectionState().querySessions.values().stream().anyMatch(session -> session.connection == jdbcConnection);
} }
private static boolean isPhoenixConnection(JsonNode connection, String url) { private static boolean isPhoenixConnection(JsonNode connection, String url) {
@@ -1034,6 +1179,8 @@ public final class DbxJdbcPlugin {
boolean bitStringColumns = usesBitStringColumns(connection); boolean bitStringColumns = usesBitStringColumns(connection);
boolean fullPrecisionIntegers = usesFullPrecisionIntegers(connection); boolean fullPrecisionIntegers = usesFullPrecisionIntegers(connection);
try (Statement statement = conn.createStatement()) { try (Statement statement = conn.createStatement()) {
connectionState().activeStatement = statement;
if (connectionState().closing) throw new SQLException("JDBC logical session is closed");
applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks); applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks);
String trimmedSql = trimStatementSql(sql); String trimmedSql = trimStatementSql(sql);
String effectiveSql = rewritePhoenixSystemCatalogQuery(connection, conn, trimmedSql); String effectiveSql = rewritePhoenixSystemCatalogQuery(connection, conn, trimmedSql);
@@ -1080,7 +1227,7 @@ public final class DbxJdbcPlugin {
private static ObjectNode beginManualTransaction(JsonNode connection, String database, String schema) private static ObjectNode beginManualTransaction(JsonNode connection, String database, String schema)
throws SQLException { throws SQLException {
if (manualTransactionActive) { if (connectionState().manualTransactionActive) {
throw new SQLException("A manual transaction is already active"); throw new SQLException("A manual transaction is already active");
} }
Connection conn = openConnection(connection); Connection conn = openConnection(connection);
@@ -1091,7 +1238,7 @@ public final class DbxJdbcPlugin {
} }
applyExecutionContext(connection, conn, database, schema); applyExecutionContext(connection, conn, database, schema);
conn.setAutoCommit(false); conn.setAutoCommit(false);
manualTransactionActive = true; connectionState().manualTransactionActive = true;
return okResult(); return okResult();
} }
@@ -1113,7 +1260,7 @@ public final class DbxJdbcPlugin {
Connection conn = activeManualTransactionConnection(null); Connection conn = activeManualTransactionConnection(null);
conn.commit(); conn.commit();
conn.setAutoCommit(true); conn.setAutoCommit(true);
manualTransactionActive = false; connectionState().manualTransactionActive = false;
return okResult(); return okResult();
} }
@@ -1121,18 +1268,19 @@ public final class DbxJdbcPlugin {
Connection conn = activeManualTransactionConnection(null); Connection conn = activeManualTransactionConnection(null);
conn.rollback(); conn.rollback();
conn.setAutoCommit(true); conn.setAutoCommit(true);
manualTransactionActive = false; connectionState().manualTransactionActive = false;
return okResult(); return okResult();
} }
private static Connection activeManualTransactionConnection(JsonNode connection) throws SQLException { private static Connection activeManualTransactionConnection(JsonNode connection) throws SQLException {
if (!manualTransactionActive || sharedConnection == null || sharedConnection.isClosed()) { JdbcConnectionState state = connectionState();
if (!state.manualTransactionActive || state.sharedConnection == null || state.sharedConnection.isClosed()) {
throw new SQLException("No manual transaction is active"); throw new SQLException("No manual transaction is active");
} }
if (connection != null && !connectionKey(connection).equals(sharedConnectionKey)) { if (connection != null && !connectionKey(connection).equals(state.sharedConnectionKey)) {
throw new SQLException("The manual transaction belongs to a different JDBC connection"); throw new SQLException("The manual transaction belongs to a different JDBC connection");
} }
return sharedConnection; return state.sharedConnection;
} }
private static ObjectNode okResult() { private static ObjectNode okResult() {
@@ -1213,11 +1361,13 @@ public final class DbxJdbcPlugin {
Statement statement; Statement statement;
try { try {
statement = createPagedQueryStatement(conn); statement = createPagedQueryStatement(conn);
connectionState().activeStatement = statement;
} catch (Exception | LinkageError error) { } catch (Exception | LinkageError error) {
restorePagedQueryTransaction(conn, restoreAutoCommit); restorePagedQueryTransaction(conn, restoreAutoCommit);
throw error; throw error;
} }
try { try {
if (connectionState().closing) throw new SQLException("JDBC logical session is closed");
applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks); applyStatementOptions(statement, maxRows, fetchSize, timeoutSecs, quirks);
String trimmedSql = trimStatementSql(sql); String trimmedSql = trimStatementSql(sql);
String effectiveSql = rewritePhoenixSystemCatalogQuery(connection, conn, trimmedSql); String effectiveSql = rewritePhoenixSystemCatalogQuery(connection, conn, trimmedSql);
@@ -1260,11 +1410,11 @@ public final class DbxJdbcPlugin {
bitStringColumns, bitStringColumns,
usesFullPrecisionIntegers(connection) usesFullPrecisionIntegers(connection)
); );
QUERY_SESSIONS.put(sessionId, session); connectionState().querySessions.put(sessionId, session);
try { try {
return readQuerySessionPage(session, pageSize); return readQuerySessionPage(session, pageSize);
} catch (Exception | LinkageError error) { } catch (Exception | LinkageError error) {
QUERY_SESSIONS.remove(sessionId); connectionState().querySessions.remove(sessionId);
throw error; throw error;
} }
} catch (Exception | LinkageError error) { } catch (Exception | LinkageError error) {
@@ -1309,7 +1459,7 @@ public final class DbxJdbcPlugin {
} }
private static JsonNode fetchQueryPage(String sessionId, int pageSize) throws SQLException { private static JsonNode fetchQueryPage(String sessionId, int pageSize) throws SQLException {
QuerySession session = QUERY_SESSIONS.get(sessionId); QuerySession session = connectionState().querySessions.get(sessionId);
if (session == null) { if (session == null) {
throw new IllegalArgumentException("Unknown query session: " + sessionId); throw new IllegalArgumentException("Unknown query session: " + sessionId);
} }
@@ -1317,6 +1467,8 @@ public final class DbxJdbcPlugin {
} }
private static JsonNode readQuerySessionPage(QuerySession session, int pageSize) throws SQLException { private static JsonNode readQuerySessionPage(QuerySession session, int pageSize) throws SQLException {
connectionState().activeStatement = session.statement;
if (connectionState().closing) throw new SQLException("JDBC logical session is closed");
int effectivePageSize = Math.max(1, pageSize); int effectivePageSize = Math.max(1, pageSize);
ArrayNode rows = MAPPER.createArrayNode(); ArrayNode rows = MAPPER.createArrayNode();
boolean truncated = false; boolean truncated = false;
@@ -1376,7 +1528,7 @@ public final class DbxJdbcPlugin {
} }
private static boolean closeQuerySession(String sessionId) { private static boolean closeQuerySession(String sessionId) {
QuerySession session = QUERY_SESSIONS.remove(sessionId); QuerySession session = connectionState().querySessions.remove(sessionId);
if (session == null) { if (session == null) {
return false; return false;
} }
@@ -1393,7 +1545,7 @@ public final class DbxJdbcPlugin {
} }
private static void closeAllQuerySessions() { private static void closeAllQuerySessions() {
List<String> sessionIds = new ArrayList<>(QUERY_SESSIONS.keySet()); List<String> sessionIds = new ArrayList<>(connectionState().querySessions.keySet());
for (String sessionId : sessionIds) { for (String sessionId : sessionIds) {
closeQuerySession(sessionId); closeQuerySession(sessionId);
} }
@@ -3504,22 +3656,23 @@ public final class DbxJdbcPlugin {
} }
private static void closeSharedConnection() { private static void closeSharedConnection() {
JdbcConnectionState state = connectionState();
closeAllQuerySessions(); closeAllQuerySessions();
if (sharedConnection != null) { if (state.sharedConnection != null) {
if (manualTransactionActive) { if (state.manualTransactionActive) {
try { try {
sharedConnection.rollback(); state.sharedConnection.rollback();
} catch (SQLException ignored) { } catch (SQLException ignored) {
} }
} }
try { try {
sharedConnection.close(); state.sharedConnection.close();
} catch (SQLException ignored) { } catch (SQLException ignored) {
} }
sharedConnection = null; state.sharedConnection = null;
sharedConnectionKey = ""; state.sharedConnectionKey = "";
} }
manualTransactionActive = false; state.manualTransactionActive = false;
} }
private static String driverKey(JsonNode connection) { private static String driverKey(JsonNode connection) {
@@ -0,0 +1,205 @@
package app.dbx.jdbc;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public final class H2FileSessionProbe {
private static final ObjectMapper JSON = new ObjectMapper();
public static void main(String[] args) throws Exception {
if (args.length != 4) throw new IllegalArgumentException("plugin.jar h2-2.1.214.jar temporary-directory baseline|patched|cold");
Path plugin = Path.of(args[0]).toAbsolutePath();
Path driver = Path.of(args[1]).toAbsolutePath();
Path directory = Path.of(args[2]).toAbsolutePath();
Files.createDirectories(directory);
ObjectNode connection = JSON.createObjectNode();
connection.put("connection_string", "jdbc:h2:file:" + directory.resolve("ownership") + ";LOCK_TIMEOUT=2000");
connection.put("jdbc_driver_class", "org.h2.Driver");
connection.putArray("jdbc_driver_paths").add(driver.toString());
connection.put("username", "sa");
if (args[3].equals("cold")) {
coldConcurrentStarts(plugin, connection, directory);
return;
}
try (Sidecar owner = new Sidecar(plugin)) {
if (args[3].equals("baseline")) {
owner.ok("connect", params(connection, null));
owner.ok("executeQuery", params(connection, null).put("sql", "CREATE TABLE IF NOT EXISTS test(id INT PRIMARY KEY)"));
try (Sidecar competitor = new Sidecar(plugin)) {
JsonNode locked = competitor.call("connect", params(connection, null));
check(locked.has("error"), "baseline unexpectedly opened the same file in two JVMs");
check(locked.path("error").path("message").asText().toLowerCase().matches("(?s).*(lock|already in use).*"), locked.toString());
System.out.println("PASS baseline metadata JVM opens file; tab JVM reports file ownership error");
}
return;
}
check(owner.ok("jdbcSessionProtocol", JSON.createObjectNode()).path("version").asInt() == 2, "logical session protocol");
for (String session : List.of("metadata", "a", "b")) {
owner.ok("openJdbcSession", params(connection, session));
owner.ok("connect", params(connection, session));
}
query(owner, connection, "metadata", "DROP TABLE IF EXISTS test");
query(owner, connection, "metadata", "CREATE TABLE test(id INT PRIMARY KEY)");
owner.ok("listTables", params(connection, "metadata").put("schema", "PUBLIC"));
for (String session : List.of("a", "b")) owner.ok("beginManualTransaction", params(connection, session));
owner.ok("executeInManualTransaction", params(connection, "a").put("sql", "INSERT INTO test VALUES(1)"));
owner.ok("executeInManualTransaction", params(connection, "b").put("sql", "INSERT INTO test VALUES(2)"));
check(count(owner, connection, "metadata") == 0, "uncommitted rows leaked");
owner.ok("commitManualTransaction", params(connection, "a"));
check(count(owner, connection, "metadata") == 1, "commit affected another transaction");
owner.ok("rollbackManualTransaction", params(connection, "b"));
check(count(owner, connection, "metadata") == 1, "rollback affected committed row");
query(owner, connection, "b", "INSERT INTO test VALUES(3),(4)");
JsonNode page = owner.ok("executeQueryPage", params(connection, "a").put("sql", "SELECT id FROM test ORDER BY id").put("pageSize", 1));
String cursor = page.path("session_id").asText();
check(owner.call("fetchQueryPage", params(connection, "b").put("sessionId", cursor)).has("error"), "foreign cursor accepted");
check(owner.ok("fetchQueryPage", params(connection, "a").put("sessionId", cursor).put("pageSize", 1)).path("rows").path(0).path(0).asInt() == 3, "cursor paging");
owner.ok("closeQuerySession", params(connection, "a").put("sessionId", cursor));
check(owner.call("fetchQueryPage", params(connection, "a").put("sessionId", cursor)).has("error"), "closed cursor accepted");
ObjectNode missing = connection.deepCopy();
missing.put("connection_string", "jdbc:h2:file:" + directory.resolve("missing") + ";IFEXISTS=TRUE");
owner.ok("openJdbcSession", params(missing, "failed"));
check(owner.call("connect", params(missing, "failed")).has("error"), "failed connection accepted");
check(count(owner, connection, "b") == 3, "failed connect broke another session");
ObjectNode other = connection.deepCopy();
other.put("connection_string", "jdbc:h2:file:" + directory.resolve("separate"));
owner.ok("openJdbcSession", params(other, "other"));
owner.ok("connect", params(other, "other"));
check(owner.call("executeQuery", params(other, "other").put("sql", "SELECT * FROM test")).has("error"), "separate files were conflated");
owner.ok("closeJdbcSession", params(other, "other"));
try (Sidecar competitor = new Sidecar(plugin)) {
check(competitor.call("connect", params(connection, null)).has("error"), "file locking was disabled");
}
owner.ok("beginManualTransaction", params(connection, "a"));
owner.ok("executeInManualTransaction", params(connection, "a").put("sql", "INSERT INTO test VALUES(99)"));
owner.send("executeInManualTransaction", params(connection, "a").put("sql", "SELECT SUM(X) FROM SYSTEM_RANGE(1, 1000000000)"));
Thread.sleep(100);
owner.send("closeJdbcSession", params(connection, "a"));
JsonNode first = owner.read();
JsonNode second = owner.read();
check(first.has("error") || second.has("error"), "cancellation did not reach the active statement");
check(count(owner, connection, "b") == 3, "cancelled transaction was not rolled back independently");
owner.ok("closeJdbcSession", params(connection, "b"));
check(count(owner, connection, "metadata") == 3, "closing tab broke metadata");
owner.ok("closeJdbcSession", params(connection, "metadata"));
System.out.println("PASS patched shared JVM: independent transactions, cursors, cancellation, failures, files and locks");
}
try (Sidecar reopened = new Sidecar(plugin)) {
reopened.ok("openJdbcSession", params(connection, "reopened"));
reopened.ok("connect", params(connection, "reopened"));
check(count(reopened, connection, "reopened") == 3, "last-runtime teardown/reopen");
reopened.ok("closeJdbcSession", params(connection, "reopened"));
}
System.out.println("PASS final runtime teardown and file reopen");
coldConcurrentStarts(plugin, connection, directory);
}
private static void coldConcurrentStarts(Path plugin, ObjectNode connection, Path directory) throws Exception {
for (int attemptIndex = 0; attemptIndex < 5; attemptIndex++) {
ObjectNode coldConnection = connection.deepCopy();
coldConnection.put("connection_string", "jdbc:h2:file:" + directory.resolve("cold-" + attemptIndex));
try (Sidecar owner = new Sidecar(plugin)) {
for (int sessionIndex = 0; sessionIndex < 8; sessionIndex++) {
owner.ok("openJdbcSession", params(coldConnection, "cold-" + sessionIndex));
}
for (int sessionIndex = 0; sessionIndex < 8; sessionIndex++) {
owner.send("connect", params(coldConnection, "cold-" + sessionIndex));
}
java.util.Set<Long> responseIds = new java.util.HashSet<>();
for (int responseIndex = 0; responseIndex < 8; responseIndex++) {
JsonNode response = owner.read();
check(!response.has("error"), "cold start: " + response);
check(responseIds.add(response.path("id").asLong()), "duplicate connect response");
}
for (int sessionIndex = 0; sessionIndex < 8; sessionIndex++) {
String sessionId = "cold-" + sessionIndex;
query(owner, coldConnection, sessionId, "SELECT 1");
owner.ok("closeJdbcSession", params(coldConnection, sessionId));
check(owner.call("connect", params(coldConnection, sessionId)).has("error"), "late connect reopened closed session");
}
}
}
System.out.println("PASS cold concurrent starts: five fresh JVMs, eight connects each, late connects rejected");
}
private static int count(Sidecar process, ObjectNode connection, String session) throws Exception {
return query(process, connection, session, "SELECT COUNT(*) FROM test").path("rows").path(0).path(0).asInt();
}
private static JsonNode query(Sidecar process, ObjectNode connection, String session, String sql) throws Exception {
return process.ok("executeQuery", params(connection, session).put("sql", sql));
}
private static ObjectNode params(ObjectNode connection, String session) {
ObjectNode params = JSON.createObjectNode();
params.set("connection", connection);
if (session != null) params.put("jdbcSessionId", session);
return params;
}
private static void check(boolean condition, String message) {
if (!condition) throw new AssertionError(message);
}
private static final class Sidecar implements AutoCloseable {
final Process process;
final BufferedReader input;
final BufferedWriter output;
long id;
Sidecar(Path plugin) throws Exception {
List<String> command = new ArrayList<>();
command.add(Path.of(System.getProperty("java.home"), "bin", "java").toString());
command.add("-jar");
command.add(plugin.toString());
process = new ProcessBuilder(command).redirectError(ProcessBuilder.Redirect.INHERIT).start();
input = new BufferedReader(new InputStreamReader(process.getInputStream(), java.nio.charset.StandardCharsets.UTF_8));
output = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), java.nio.charset.StandardCharsets.UTF_8));
}
void send(String method, ObjectNode params) throws Exception {
ObjectNode request = JSON.createObjectNode().put("id", ++id).put("method", method);
request.set("params", params);
output.write(request.toString());
output.newLine();
output.flush();
}
JsonNode read() throws Exception {
String line = input.readLine();
check(line != null, "JDBC process exited before responding");
return JSON.readTree(line);
}
JsonNode call(String method, ObjectNode params) throws Exception { send(method, params); return read(); }
JsonNode ok(String method, ObjectNode params) throws Exception {
JsonNode response = call(method, params);
check(!response.has("error"), response.toString());
return response.path("result");
}
public void close() throws Exception {
if (process.isAlive()) {
call("close", JSON.createObjectNode());
if (!process.waitFor(5, TimeUnit.SECONDS)) {
process.destroyForcibly();
throw new AssertionError("JDBC runtime did not terminate");
}
}
input.close();
output.close();
}
}
}
@@ -0,0 +1,431 @@
package app.dbx.jdbc;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.*;
final class H2LogicalSessionTest {
static final ObjectMapper MAPPER = new ObjectMapper();
static final List<Physical> PHYSICAL = new CopyOnWriteArrayList<>();
final List<String> sessions = new ArrayList<>();
@AfterEach
void cleanup() throws Exception {
for (String session : sessions) request(session, "closeJdbcSession", null);
request(null, "close", null);
var driverKey = DbxJdbcPlugin.class.getDeclaredField("logicalDriverKey");
driverKey.setAccessible(true);
driverKey.set(null, null);
PHYSICAL.clear();
}
@Test
void coldConcurrentConnectsShareTheFirstDriverRegistration() throws Exception {
var driverField = DbxJdbcPlugin.class.getDeclaredField("registeredDriver");
driverField.setAccessible(true);
driverField.set(null, null);
var pending = new ArrayList<java.util.concurrent.CompletableFuture<JsonNode>>();
synchronized (DbxJdbcPlugin.class) {
for (int sessionIndex = 0; sessionIndex < 8; sessionIndex++) {
String sessionId = "cold-" + sessionIndex;
sessions.add(sessionId);
ok(request(sessionId, "openJdbcSession", null));
pending.add(java.util.concurrent.CompletableFuture.supplyAsync(() -> {
try { return request(sessionId, "connect", null); }
catch (Exception error) { throw new RuntimeException(error); }
}));
}
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (logicalSessions().size() != 8 && System.nanoTime() < deadline) Thread.yield();
assertEquals(8, logicalSessions().size());
}
for (var response : pending) ok(response.get(5, TimeUnit.SECONDS));
assertEquals(8, PHYSICAL.size());
}
private static java.util.Map<?, ?> logicalSessions() throws Exception {
var field = DbxJdbcPlugin.class.getDeclaredField("LOGICAL_SESSIONS");
field.setAccessible(true);
return (java.util.Map<?, ?>) field.get(null);
}
@Test
void closedAndFailedSessionsDoNotAccumulateOrReopenOnLateRequests() throws Exception {
connect("metadata");
ok(request("metadata", "beginManualTransaction", null));
for (int cycleIndex = 0; cycleIndex < 200; cycleIndex++) {
String sessionId = "cycle-" + cycleIndex;
connect(sessionId);
ok(request(sessionId, "closeJdbcSession", null));
ok(request(sessionId, "closeJdbcSession", null));
assertTrue(request(sessionId, "connect", null).has("error"));
assertTrue(request(sessionId, "executeQuery", "SELECT 1").has("error"));
ObjectNode failed = params("failed-" + cycleIndex);
ok(call("openJdbcSession", failed));
((ObjectNode) failed.path("connection")).put("connection_string", "jdbc:h2:file:fail");
assertTrue(call("connect", failed).has("error"));
assertEquals(1, logicalSessions().size());
}
assertEquals(201, PHYSICAL.size());
assertFalse(PHYSICAL.get(0).autoCommit);
assertFalse(PHYSICAL.get(0).closed);
ok(request("metadata", "rollbackManualTransaction", null));
ok(request("metadata", "closeJdbcSession", null));
assertTrue(logicalSessions().isEmpty());
}
@Test
void closeInvalidatesAConnectRetainedAtIngressBeforeItRuns() throws Exception {
ok(request("queued", "openJdbcSession", null));
ObjectNode queued = MAPPER.createObjectNode().put("id", 1).put("method", "connect");
queued.set("params", params("queued"));
Method retain = DbxJdbcPlugin.class.getDeclaredMethod("retainLogicalState", JsonNode.class);
retain.setAccessible(true);
Object retained = retain.invoke(null, queued);
ok(request("queued", "closeJdbcSession", null));
assertTrue(logicalSessions().isEmpty());
Method handler = DbxJdbcPlugin.class.getDeclaredMethod("handleRequest", JsonNode.class, retained.getClass());
handler.setAccessible(true);
assertTrue(((JsonNode) handler.invoke(null, queued, retained)).has("error"));
assertTrue(request("queued", "connect", null).has("error"));
assertTrue(PHYSICAL.isEmpty());
}
@Test
void closeDuringPhysicalConnectClosesOnlyItsRetainedConnection() throws Exception {
connect("survivor");
ok(request("in-flight", "openJdbcSession", null));
FakeDriver.connectStarted = new CountDownLatch(1);
FakeDriver.connectReleased = new CountDownLatch(1);
ObjectNode connectingParams = params("in-flight");
((ObjectNode) connectingParams.path("connection")).put("connection_string", "jdbc:h2:file:slow");
var connecting = java.util.concurrent.CompletableFuture.supplyAsync(() -> {
try { return call("connect", connectingParams); }
catch (Exception error) { throw new RuntimeException(error); }
});
assertTrue(FakeDriver.connectStarted.await(5, TimeUnit.SECONDS));
var closing = java.util.concurrent.CompletableFuture.supplyAsync(() -> {
try { return request("in-flight", "closeJdbcSession", null); }
catch (Exception error) { throw new RuntimeException(error); }
});
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (logicalSessions().size() != 1 && System.nanoTime() < deadline) Thread.yield();
assertEquals(1, logicalSessions().size());
assertTrue(request("in-flight", "connect", null).has("error"));
ok(request("survivor", "executeQuery", "SELECT 1"));
FakeDriver.connectReleased.countDown();
connecting.get(5, TimeUnit.SECONDS);
ok(closing.get(5, TimeUnit.SECONDS));
assertEquals(2, PHYSICAL.size());
assertTrue(PHYSICAL.get(1).closed);
assertFalse(PHYSICAL.get(0).closed);
}
@Test
void incompatibleDriverCannotReplaceTheLiveDriverOrClassLoader() throws Exception {
connect("owner");
var loaderField = DbxJdbcPlugin.class.getDeclaredField("registeredDriverClassLoader");
loaderField.setAccessible(true);
Object loader = loaderField.get(null);
ObjectNode incompatible = params("different");
ok(call("openJdbcSession", incompatible));
((ObjectNode) incompatible.path("connection")).put("jdbc_driver_class", "different.Driver");
assertTrue(call("connect", incompatible).path("error").path("message").asText().contains("same selected driver"));
assertSame(loader, loaderField.get(null));
assertEquals(1, logicalSessions().size());
assertFalse(PHYSICAL.get(0).closed);
ok(request("owner", "executeQuery", "SELECT 1"));
}
@Test
void isolatesConnectionsTransactionsAndCursorOwnership() throws Exception {
connect("metadata");
connect("tab-a");
connect("tab-b");
assertEquals(3, PHYSICAL.size());
ok(request("tab-a", "beginManualTransaction", null));
assertFalse(PHYSICAL.get(1).autoCommit);
assertTrue(PHYSICAL.get(2).autoCommit);
ok(request("tab-b", "beginManualTransaction", null));
ok(request("tab-a", "commitManualTransaction", null));
assertEquals(1, PHYSICAL.get(1).commits);
assertEquals(0, PHYSICAL.get(2).commits);
ok(request("tab-b", "rollbackManualTransaction", null));
assertEquals(1, PHYSICAL.get(2).rollbacks);
JsonNode page = request("tab-a", "executeQueryPage", "SELECT 1");
ok(page);
String cursor = page.path("result").path("session_id").asText();
assertFalse(cursor.isBlank());
ObjectNode params = params("tab-b").put("sessionId", cursor);
assertTrue(call("fetchQueryPage", params).has("error"));
params.put("jdbcSessionId", "tab-a");
JsonNode next = call("fetchQueryPage", params);
ok(next);
assertEquals(2, next.path("result").path("rows").path(0).path(0).asInt());
ok(call("closeQuerySession", params));
assertTrue(call("fetchQueryPage", params).has("error"));
ok(request("tab-a", "closeJdbcSession", null));
assertTrue(PHYSICAL.get(1).closed);
assertFalse(PHYSICAL.get(0).closed);
assertFalse(PHYSICAL.get(2).closed);
ok(request("tab-b", "executeQuery", "SELECT 1"));
}
@Test
void failedConnectAndCancellationDoNotCloseOtherSessions() throws Exception {
connect("survivor");
ObjectNode bad = params("failed");
ok(call("openJdbcSession", bad));
((ObjectNode) bad.path("connection")).put("connection_string", "jdbc:h2:file:fail");
assertTrue(call("connect", bad).has("error"));
assertFalse(PHYSICAL.get(0).closed);
connect("cancelled");
Physical cancelled = PHYSICAL.get(1);
var query = java.util.concurrent.CompletableFuture.supplyAsync(() -> {
try { return request("cancelled", "executeQuery", "WAIT"); }
catch (Exception error) { throw new RuntimeException(error); }
});
assertTrue(cancelled.started.await(5, TimeUnit.SECONDS));
ok(request("survivor", "executeQuery", "SELECT 1"));
ok(request("cancelled", "closeJdbcSession", null));
assertTrue(query.get(5, TimeUnit.SECONDS).has("error"));
assertTrue(cancelled.closed);
assertFalse(PHYSICAL.get(0).closed);
assertTrue(request("cancelled", "executeQuery", "SELECT 1").has("error"));
ok(request("survivor", "executeQuery", "SELECT 1"));
}
@Test
void processColdStartAcceptsEightConnectsBeforeResponsesAreRead() throws Exception {
Process process = startProcess();
try (var writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(process.getOutputStream()));
var reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()))) {
for (int sessionIndex = 0; sessionIndex < 8; sessionIndex++) {
send(writer, sessionIndex, "openJdbcSession", params("cold-" + sessionIndex));
ok(read(reader));
}
for (int sessionIndex = 0; sessionIndex < 8; sessionIndex++) {
send(writer, sessionIndex + 8, "connect", params("cold-" + sessionIndex));
}
java.util.Set<Integer> responseIds = new java.util.HashSet<>();
for (int responseIndex = 0; responseIndex < 8; responseIndex++) {
JsonNode response = read(reader);
ok(response);
assertTrue(responseIds.add(response.path("id").asInt()));
}
send(writer, 16, "close", MAPPER.createObjectNode());
ok(read(reader));
assertTrue(process.waitFor(5, TimeUnit.SECONDS));
assertEquals(0, process.exitValue());
} finally {
process.destroyForcibly();
}
}
private Process startProcess() throws Exception {
java.util.Set<String> paths = new java.util.LinkedHashSet<>();
for (Class<?> type : List.of(DbxJdbcPlugin.class, getClass(), ObjectMapper.class,
com.fasterxml.jackson.core.JsonFactory.class, com.fasterxml.jackson.annotation.JsonProperty.class)) {
paths.add(java.nio.file.Path.of(type.getProtectionDomain().getCodeSource().getLocation().toURI()).toString());
}
for (ClassLoader loader = getClass().getClassLoader(); loader != null; loader = loader.getParent()) {
if (loader instanceof java.net.URLClassLoader urls) {
for (java.net.URL url : urls.getURLs()) paths.add(java.nio.file.Path.of(url.toURI()).toString());
}
}
return new ProcessBuilder(
java.nio.file.Path.of(System.getProperty("java.home"), "bin", "java").toString(),
"-cp", String.join(java.io.File.pathSeparator, paths), DbxJdbcPlugin.class.getName()
).redirectError(ProcessBuilder.Redirect.INHERIT).start();
}
@Test
void processDispatchesOtherSessionsAndCancellationWhileQueryRuns() throws Exception {
Process process = startProcess();
try (var writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(process.getOutputStream()));
var reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()))) {
send(writer, 10, "openJdbcSession", params("a"));
ok(read(reader));
send(writer, 1, "connect", params("a"));
ok(read(reader));
send(writer, 11, "openJdbcSession", params("b"));
ok(read(reader));
send(writer, 2, "connect", params("b"));
ok(read(reader));
send(writer, 3, "executeQuery", params("a").put("sql", "WAIT"));
send(writer, 4, "executeQuery", params("b").put("sql", "SELECT 1"));
JsonNode survivor = read(reader);
assertEquals(4, survivor.path("id").asInt());
ok(survivor);
send(writer, 5, "closeJdbcSession", params("a"));
JsonNode first = read(reader);
JsonNode second = read(reader);
assertTrue(first.has("error") || second.has("error"));
send(writer, 6, "closeJdbcSession", params("b"));
ok(read(reader));
send(writer, 7, "close", MAPPER.createObjectNode());
ok(read(reader));
assertTrue(process.waitFor(5, TimeUnit.SECONDS));
assertEquals(0, process.exitValue());
} finally {
process.destroyForcibly();
}
}
private static void send(java.io.BufferedWriter writer, int id, String method, ObjectNode params) throws Exception {
ObjectNode request = MAPPER.createObjectNode().put("id", id).put("method", method);
request.set("params", params);
writer.write(request.toString());
writer.newLine();
writer.flush();
}
private static JsonNode read(java.io.BufferedReader reader) throws Exception {
String line = java.util.concurrent.CompletableFuture.supplyAsync(() -> {
try { return reader.readLine(); }
catch (java.io.IOException error) { throw new RuntimeException(error); }
}).get(5, TimeUnit.SECONDS);
assertNotNull(line);
return MAPPER.readTree(line);
}
void connect(String session) throws Exception {
sessions.add(session);
ok(request(session, "openJdbcSession", null));
ok(request(session, "connect", null));
}
static void ok(JsonNode response) { assertFalse(response.has("error"), response.toString()); }
static ObjectNode params(String session) {
ObjectNode config = MAPPER.createObjectNode();
config.put("connection_string", "jdbc:h2:file:sample");
config.put("jdbc_driver_class", FakeDriver.class.getName());
ObjectNode params = MAPPER.createObjectNode();
params.set("connection", config);
if (session != null) params.put("jdbcSessionId", session);
params.put("pageSize", 1);
return params;
}
static JsonNode request(String session, String method, String sql) throws Exception {
ObjectNode params = params(session);
if (sql != null) params.put("sql", sql);
return call(method, params);
}
static JsonNode call(String name, ObjectNode params) throws Exception {
Method handler = DbxJdbcPlugin.class.getDeclaredMethod("handleLine", String.class);
handler.setAccessible(true);
ObjectNode request = MAPPER.createObjectNode().put("id", 1).put("method", name);
request.set("params", params);
return (JsonNode) handler.invoke(null, request.toString());
}
public static final class FakeDriver implements Driver {
static volatile CountDownLatch connectStarted;
static volatile CountDownLatch connectReleased;
public Connection connect(String url, Properties properties) throws SQLException {
if (!acceptsURL(url)) return null;
if (url.endsWith("fail")) throw new SQLException("connect failed");
if (url.endsWith("slow")) {
connectStarted.countDown();
try {
if (!connectReleased.await(5, TimeUnit.SECONDS)) throw new SQLException("connect timed out");
} catch (InterruptedException error) {
throw new SQLException(error);
}
}
Physical physical = new Physical();
PHYSICAL.add(physical);
return physical.connection();
}
public boolean acceptsURL(String url) { return url.startsWith("jdbc:h2:file:"); }
public DriverPropertyInfo[] getPropertyInfo(String url, Properties properties) { return new DriverPropertyInfo[0]; }
public int getMajorVersion() { return 1; }
public int getMinorVersion() { return 0; }
public boolean jdbcCompliant() { return true; }
public Logger getParentLogger() { return Logger.getGlobal(); }
}
static final class Physical {
boolean autoCommit = true;
boolean closed;
int commits;
int rollbacks;
final CountDownLatch started = new CountDownLatch(1);
final CountDownLatch cancelled = new CountDownLatch(1);
Connection connection() {
return proxy(Connection.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "getAutoCommit" -> autoCommit;
case "setAutoCommit" -> { autoCommit = (boolean) arguments[0]; yield null; }
case "isClosed" -> closed;
case "close" -> { closed = true; yield null; }
case "commit" -> { commits++; yield null; }
case "rollback" -> { rollbacks++; yield null; }
case "createStatement" -> statement();
default -> defaultValue(invokedMethod.getReturnType());
});
}
Statement statement() {
ResultSet rs = resultSet();
return proxy(Statement.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "execute" -> {
if ("WAIT".equals(arguments[0])) {
started.countDown();
if (!cancelled.await(5, TimeUnit.SECONDS)) throw new SQLException("cancel timed out");
throw new SQLException("statement cancelled");
}
yield true;
}
case "getResultSet" -> rs;
case "cancel" -> { cancelled.countDown(); yield null; }
default -> defaultValue(invokedMethod.getReturnType());
});
}
ResultSet resultSet() {
int[] row = { 0 };
ResultSetMetaData meta = proxy(ResultSetMetaData.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "getColumnCount" -> 1;
case "getColumnType" -> Types.INTEGER;
case "getColumnName", "getColumnLabel" -> "value";
default -> defaultValue(invokedMethod.getReturnType());
});
return proxy(ResultSet.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "next" -> ++row[0] <= 3;
case "getMetaData" -> meta;
case "getObject" -> row[0];
default -> defaultValue(invokedMethod.getReturnType());
});
}
}
static <T> T proxy(Class<T> type, java.lang.reflect.InvocationHandler handler) {
return type.cast(Proxy.newProxyInstance(H2LogicalSessionTest.class.getClassLoader(), new Class<?>[] { type }, handler));
}
static Object defaultValue(Class<?> type) {
if (type == boolean.class) return false;
if (type == int.class) return 0;
if (type == long.class) return 0L;
return null;
}
}
@@ -40,8 +40,8 @@ final class InterSystemsNumericTest {
Fixture fixture = new Fixture(url, type, values, null); Fixture fixture = new Fixture(url, type, values, null);
JsonNode query = fixture.query("executeQuery"); JsonNode query = fixture.query("executeQuery");
assertFalse(query.has("error"), query.toString()); assertFalse(query.has("error"), query.toString());
for (int i = 0; i < values.length; i++) { for (int rowIndex = 0; rowIndex < values.length; rowIndex++) {
assertValue(values[i], query.path("result").path("rows").path(i).path(0)); assertValue(values[rowIndex], query.path("result").path("rows").path(rowIndex).path(0));
} }
JsonNode page = fixture.query("executeQueryPage"); JsonNode page = fixture.query("executeQueryPage");
int row = 0; int row = 0;
@@ -137,12 +137,12 @@ final class InterSystemsNumericTest {
} }
void install() throws Exception { void install() throws Exception {
Connection connection = proxy(Connection.class, (p, m, a) -> switch (m.getName()) { Connection connection = proxy(Connection.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "createStatement" -> statement(); case "createStatement" -> statement();
case "getAutoCommit" -> true; case "getAutoCommit" -> true;
default -> defaultValue(m.getReturnType()); default -> defaultValue(invokedMethod.getReturnType());
}); });
set("registeredDriver", proxy(Driver.class, (p, m, a) -> defaultValue(m.getReturnType()))); set("registeredDriver", proxy(Driver.class, (proxyInstance, invokedMethod, arguments) -> defaultValue(invokedMethod.getReturnType())));
set("registeredDriverKey", key("driverKey")); set("registeredDriverKey", key("driverKey"));
set("sharedConnection", connection); set("sharedConnection", connection);
set("sharedConnectionKey", key("connectionKey")); set("sharedConnectionKey", key("connectionKey"));
@@ -166,23 +166,23 @@ final class InterSystemsNumericTest {
Statement statement() { Statement statement() {
ResultSet result = resultSet(); ResultSet result = resultSet();
return proxy(Statement.class, (p, m, a) -> switch (m.getName()) { return proxy(Statement.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "execute" -> true; case "execute" -> true;
case "getResultSet", "executeQuery" -> result; case "getResultSet", "executeQuery" -> result;
case "close" -> { closedStatements++; yield null; } case "close" -> { closedStatements++; yield null; }
default -> defaultValue(m.getReturnType()); default -> defaultValue(invokedMethod.getReturnType());
}); });
} }
ResultSet resultSet() { ResultSet resultSet() {
int[] row = { -1 }; int[] row = { -1 };
ResultSetMetaData meta = proxy(ResultSetMetaData.class, (p, m, a) -> switch (m.getName()) { ResultSetMetaData meta = proxy(ResultSetMetaData.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "getColumnCount" -> 1; case "getColumnCount" -> 1;
case "getColumnType" -> type; case "getColumnType" -> type;
case "getColumnLabel", "getColumnName" -> "value"; case "getColumnLabel", "getColumnName" -> "value";
default -> defaultValue(m.getReturnType()); default -> defaultValue(invokedMethod.getReturnType());
}); });
return proxy(ResultSet.class, (p, m, a) -> switch (m.getName()) { return proxy(ResultSet.class, (proxyInstance, invokedMethod, arguments) -> switch (invokedMethod.getName()) {
case "next" -> ++row[0] < values.length; case "next" -> ++row[0] < values.length;
case "getMetaData" -> meta; case "getMetaData" -> meta;
case "getBigDecimal" -> { case "getBigDecimal" -> {
@@ -205,7 +205,7 @@ final class InterSystemsNumericTest {
case "getString" -> values[row[0]]; case "getString" -> values[row[0]];
case "getBoolean" -> true; case "getBoolean" -> true;
case "getBytes" -> new byte[] { 1, 2 }; case "getBytes" -> new byte[] { 1, 2 };
default -> defaultValue(m.getReturnType()); default -> defaultValue(invokedMethod.getReturnType());
}); });
} }
} }
@@ -219,9 +219,17 @@ final class InterSystemsNumericTest {
} }
private static void set(String name, Object value) throws Exception { private static void set(String name, Object value) throws Exception {
Field field = DbxJdbcPlugin.class.getDeclaredField(name); Object target = null;
Class<?> owner = DbxJdbcPlugin.class;
if (name.startsWith("sharedConnection")) {
Method method = owner.getDeclaredMethod("connectionState");
method.setAccessible(true);
target = method.invoke(null);
owner = target.getClass();
}
Field field = owner.getDeclaredField(name);
field.setAccessible(true); field.setAccessible(true);
field.set(null, value); field.set(target, value);
} }
private static <T> T proxy(Class<T> type, InvocationHandler handler) { private static <T> T proxy(Class<T> type, InvocationHandler handler) {