Skip to content

Add try-with-resources to methods to insert BLOB #1654

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 2 commits into from
Oct 30, 2020

Conversation

k-tamura
Copy link
Contributor

@k-tamura k-tamura commented Jun 29, 2020

Problem

If using Oracle DB as a session store of Spring Session and idle connection timeout is not set (or connections do not become idle for a long time), there is a possibility that Oracle's TEMP space is increased and finally SQLException (ORA-01652) is thrown.

Note: This problem has already reported on #1321.

Description

When inserting a record to SPRING_SESSION_ATTRIBUTES table (which has Blob column), the following steps are executed:

  1. Create a temporary LOB in Oracle's TEMP space
  2. Insert a record to SPRING_SESSION_ATTRIBUTES table

The created temporary LOB is freed when the connection (which created the temporary LOB) is closed by idle timeout or Spring application is restarted, but these events do not always occur.

Solution

So the following step should be added just after step 2.

  1. Free the temporary LOB

To achieve this, try-with-resources statement should be added.

This problem is caused by leaks of calling TemporaryLobCreator.close(). TemporaryLobCreator implements LobCreator (which extends Closeable) and has close() (which frees temporary LOB), but TemporaryLobCreator is created without try-with-resources statement. Therefor close() is not called and temporary LOB is not freed.

Steps to reproduce:

  1. Create a Web application which uses Oracle DB as a session store of Spring Session.
  2. Access to the application (for example, 1000 times by JMeter).
  3. Run the SQL:
    select * from V$TEMPORARY_LOBS;
    

If you keep accessing to the application, ORA-01652 occurs and SQLException is thrown.

Result before applying my commit (CACHE_LOBS will increase):

   SID CACHE_LOBS NOCACHE_LOBS ABSTRACT_LOBS     CON_ID
---------- ---------- ------------ ------------- ----------
     18	  371		 0	       0	  0
     23	    0		 0	       0	  0
     25	  229		 0	       0	  0
    173	  144		 0	       0	  0
    178	  723		 0	       0	  0
    346	  533		 0	       0	  0

Result after applying my commit (CACHE_LOBS will not increase):

    SID CACHE_LOBS NOCACHE_LOBS ABSTRACT_LOBS     CON_ID
---------- ---------- ------------ ------------- ----------
     23	    0		 0	       0	  0

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 29, 2020
Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Can you please add some tests for the changes?

@k-tamura
Copy link
Contributor Author

k-tamura commented Jul 1, 2020

Hi @rwinch ,

Thank you for your reviewing.
My changes will only affect Oracle's TEMP space. So I think it is difficult to create test cases to confirm it. Do you have a good idea?

@rwinch
Copy link
Member

rwinch commented Jul 1, 2020

I'm guessing you could add a unit test to ensure that the temporary LOG is freed?

@k-tamura
Copy link
Contributor Author

k-tamura commented Jul 2, 2020

Some test methods in JdbcIndexedSessionRepositoryTests calls two methods which is changed by me. However, it seems that variable this.jdbcOperations does not include information which indicates the temporary LOG is freed even if createTemporaryLob flag is true. So I think a unit test to ensure it cannot be added.

Screenshot from 2020-07-02 22-07-35

@rwinch
Copy link
Member

rwinch commented Jul 2, 2020

I think you would need to verify that the lob that was created (which would be a mock also) is then closed proeprly.

@k-tamura
Copy link
Contributor Author

k-tamura commented Jul 9, 2020

I am sure that freeing temporary LOB cannot be tested. If not using Oracle DB, we need to test if TemporaryLobCreator.close() is called while calling this.repository.save(session); but, as far as I investigated, it is not possible on this testing framework.

@k-tamura
Copy link
Contributor Author

@rwinch

Do you believe it is possible? If yes, could you give me some more specific advice?

@rwinch
Copy link
Member

rwinch commented Jul 21, 2020

I think it should be possible by injecting a mock LobHandler that returns a mock LobCreator. Then you would verify the LobCreator is closed.

@k-tamura
Copy link
Contributor Author

@rwinch

Thank you for your advice. I added a test method. Is this what you intended?

@pivotal-issuemaster
Copy link

@k-tamura Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@k-tamura Thank you for signing the Contributor License Agreement!

@k-tamura
Copy link
Contributor Author

k-tamura commented Aug 24, 2020

@rwinch

It would be very helpful if you could comment my test case. I added it on 22 Jul.

Best regards,

@k-tamura k-tamura requested a review from rwinch September 15, 2020 04:55
@yoshimi0227
Copy link

How's the review going?
I'd really appreciate it if this request could be merged !

@k-tamura
Copy link
Contributor Author

@rwinch @vpavic

Is it difficult to include this commit in the next release?

@rwinch rwinch added in: jdbc type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 28, 2020
@rwinch rwinch added this to the 2.4.1 milestone Oct 28, 2020
@rwinch
Copy link
Member

rwinch commented Oct 28, 2020

We are already in the release process. Given this is a bug, we can get it in the next patch release (Dec 9th). That will be in time for Spring Boot's GA release.

@vpavic vpavic self-assigned this Oct 28, 2020
@eleftherias eleftherias merged commit 29ff2e4 into spring-projects:master Oct 30, 2020
@eleftherias
Copy link
Contributor

Thanks for the PR @k-tamura! This is merged into master.

eleftherias added a commit that referenced this pull request Oct 30, 2020
@k-tamura
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants