Skip to content

Calling stored procedures can leave open cursors #2915

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

Closed
Timmeeeey opened this issue Apr 14, 2023 · 2 comments
Closed

Calling stored procedures can leave open cursors #2915

Timmeeeey opened this issue Apr 14, 2023 · 2 comments
Assignees
Labels
in: repository Repositories abstraction type: bug A general bug

Comments

@Timmeeeey
Copy link

Executing multiple stored procedures via @Procedure can lead to an oracle exception (ORA-01000: maximum open cursors exceeded) because Hibernate keeps cursors open until the statement is closed.

Example:

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long> {
    @Procedure(procedureName = "myprocedure", outputParameterName = "output")
    int myProcedure();
}

To free the resources following code has to be executed after the outputs are retrieved:

StoredProcedureQuery query = ...;
try {
    query.execute();
    Long output = (Long) query .getOutputParameterValue("output");
} finally {
    query.unwrap(ProcedureOutputs.class).release(); // <- releases resources
}

This probably has to be implemented in following class:
org.springframework.data.jpa.repository.query.JpaQueryExecution.ProcedureExecution

see https://vladmihalcea.com/best-way-call-stored-procedure-jpa-hibernate/

I'm using spring-data-jpa 2.7.10

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 14, 2023
@gregturn
Copy link
Contributor

gregturn commented May 4, 2023

Looks like Vlad also succeeded at adding AutoCloseable to Hibernate 6's ProcedureCall via https://hibernate.atlassian.net/browse/HHH-13215 (back in 2019).

Trying to mimic the pattern shown in the article is actually convoluted considering we support both Hibernate and EclipseLink. However, we can detect the existence of AutoCloseable and simply invoke it, ensuring proper cleanup is performed.

@gregturn gregturn added this to the 3.1 GA (2023.0.0) milestone May 4, 2023
@gregturn gregturn added type: bug A general bug in: repository Repositories abstraction and removed status: waiting-for-triage An issue we've not yet triaged labels May 4, 2023
gregturn added a commit that referenced this issue May 4, 2023
If the JPA provider's implementation of StoredProcedureQuery implements AutoCloseable, invoke the close method when completed.

Resolves #2915.
@gregturn
Copy link
Contributor

gregturn commented May 4, 2023

Since this involves Hibernate 6, this should also be backported to Spring Data JPA 3.0 as well.

@schauder schauder removed this from the 3.1 GA (2023.0.0) milestone May 12, 2023
@mp911de mp911de changed the title Calling stored procedures can lead to ORA-01000 exception Calling stored procedures can leave open cursors May 30, 2023
mp911de pushed a commit that referenced this issue May 30, 2023
If the JPA provider's implementation of StoredProcedureQuery implements AutoCloseable, invoke the close method when completed.

Resolves #2915.
Original pull request: #2938
mp911de added a commit that referenced this issue May 30, 2023
Simplify code flow. Introduce flag to capture whether a stored procedure uses collection return types.

Remove unconditionally the Optional converter as we're already on Java 17 and do not require the Java 8 guard.

See #2915
Original pull request: #2938
mp911de pushed a commit that referenced this issue May 30, 2023
If the JPA provider's implementation of StoredProcedureQuery implements AutoCloseable, invoke the close method when completed.

Resolves #2915.
Original pull request: #2938
mp911de added a commit that referenced this issue May 30, 2023
Simplify code flow. Introduce flag to capture whether a stored procedure uses collection return types.

Remove unconditionally the Optional converter as we're already on Java 8 and do not require the Java 8 guard.

See #2915
Original pull request: #2938
@mp911de mp911de added this to the 2.7.13 (2021.2.13) milestone May 30, 2023
mp911de pushed a commit that referenced this issue May 30, 2023
If the JPA provider's implementation of StoredProcedureQuery implements AutoCloseable, invoke the close method when completed.

Resolves #2915.
Original pull request: #2938
mp911de added a commit that referenced this issue May 30, 2023
Simplify code flow. Introduce flag to capture whether a stored procedure uses collection return types.

Remove unconditionally the Optional converter as we're already on Java 17 and do not require the Java 8 guard.

See #2915
Original pull request: #2938
mp911de added a commit that referenced this issue May 30, 2023
Simplify code flow. Introduce flag to capture whether a stored procedure uses collection return types.

Remove unconditionally the Optional converter as we're already on Java 17 and do not require the Java 8 guard.

See #2915
Original pull request: #2938
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: bug A general bug
Projects
None yet
5 participants