Skip to content

Introduce explicity resource management to Transaction #1155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ class Transaction {
}
}

// eslint-disable-next-line
// @ts-ignore
[Symbol.asyncDispose] (): Promise<void> {
return this.close()
}

_onErrorCallback (error: Error): Promise<Connection | null> {
// error will be "acknowledged" by sending a RESET message
// database will then forget about this transaction and cleanup all corresponding resources
Expand Down
6 changes: 6 additions & 0 deletions packages/neo4j-driver-deno/lib/core/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ class Transaction {
}
}

// eslint-disable-next-line
// @ts-ignore
[Symbol.asyncDispose] (): Promise<void> {
return this.close()
}

_onErrorCallback (error: Error): Promise<Connection | null> {
// error will be "acknowledged" by sending a RESET message
// database will then forget about this transaction and cleanup all corresponding resources
Expand Down
41 changes: 41 additions & 0 deletions packages/neo4j-driver-deno/test/neo4j.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* limitations under the License.
*/
import neo4j from '../lib/mod.ts'
//@ts-ignore
import { assertEquals } from "https://deno.land/std@0.182.0/testing/asserts.ts";

const env = Deno.env.toObject()

Expand All @@ -42,3 +44,42 @@ Deno.test('driver.session should be able to use explicity resource management',

await session.executeRead(tx => "RETURN 1")
})


// Deno will fail with resource leaks
Deno.test('session.beginTransaction should rollback the transaction if not committed', async () => {
await using driver = neo4j.driver(uri, authToken)
await using session = driver.session()
const name = "Must Be Conor"


{
await using tx = session.beginTransaction()
await tx.run('CREATE (p:Person { name:$name }) RETURN p', { name }).summary()
}

const { records } = await driver.executeQuery('MATCH (p:Person { name:$name }) RETURN p', { name })
assertEquals(records.length, 0)
})


// Deno will fail with resource leaks
Deno.test('session.beginTransaction should noop if resource committed', async () => {
await using driver = neo4j.driver(uri, authToken)
try {
await using session = driver.session()
const name = "Must Be Conor"

{
await using tx = session.beginTransaction()
await tx.run('CREATE (p:Person { name:$name }) RETURN p', { name }).summary()
await tx.commit()
}

const { records } = await driver.executeQuery('MATCH (p:Person { name:$name }) RETURN p', { name })
assertEquals(records.length, 1)
} finally {
// cleaning up
await driver.executeQuery('MATCH (p:Person { name:$name }) DELETE(p)', { name })
}
})