Skip to content

Conversation

@andreitokar
Copy link
Contributor

Clear LocalSession.database and LocalSession. User fields to allow for in-memory data to be collected upon database closer in presence of dangling JDBC statements / connections.
Use only in-line LOBs in case of in-memory database.
Fixes #3633

… GC upon database closer in presence of dangling JDBC statements / connections
this.databaseName = databaseName;
this.databaseShortName = parseDatabaseShortName();
this.maxLengthInplaceLob = Constants.DEFAULT_MAX_LENGTH_INPLACE_LOB;
this.maxLengthInplaceLob = persistent ? Constants.DEFAULT_MAX_LENGTH_INPLACE_LOB : Integer.MAX_VALUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new byte[Integer.MAX_VALUE throws java.lang.OutOfMemoryError: Requested array size exceeds VM limit.

At least in some JVMs maximum length of byte[] is Integer.MAX_VALUE - 2, but in sources of Java Integer.MAX_VALUE - 8 is used in ByteArrayOutputStream and other places, because actual limit may differ between JVMs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think that that new byte[Integer.MAX_VALUE] will ever be executed? This is just a threshold value below which LOBs will be allocated in memory, but allocation will be done with actual size. In my mind, if someone decided to have in-memory database with LOBs, at least they have to be of some reasonable sizes. How many 2Tb LOBs will fit into JVMheap anyway? But if you think it matters, so be it, Integer.MAX_VALUE - 8

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because some application can execute something like

Connection c = DriverManager.getConnection("jdbc:h2:mem:"));
Statement s = c.createStatement();
s.execute("CREATE TABLE TEST(B BLOB)");
PreparedStatement ps = c.prepareStatement("INSERT INTO TEST VALUES ?");
ps.setBinaryStream(1, new ByteArrayInputStream(new byte[1000]), Integer.MAX_VALUE);
ps.executeUpdate();

With maxLengthInplaceLob == Integer.MAX_VALUE it will lead to that exception.

@andreitokar andreitokar merged commit 9bd7a72 into h2database:master Sep 27, 2022
@anvo1115
Copy link

anvo1115 commented Jun 30, 2023

When will h2database be released? We need this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak in case of in-memory database

3 participants