-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't automatically create an EntityManager outside of a UnitOfWork, …
…because it leads to leaks. Fixes #739, fixes #997, fixes #730, fixes #985, fixes #959, fixes #731. This also adds an optional Options to the JpaPersistModule constructor, if users wish to use the legacy behavior. This is a breaking change, but to a better default value. Users who want to keep the dangerous form can opt back in with the options. PiperOrigin-RevId: 525852009
- Loading branch information
Showing
15 changed files
with
149 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
extensions/persist/src/com/google/inject/persist/jpa/JpaPersistOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2023 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.inject.persist.jpa; | ||
|
||
/** Options that configure how the JPA persist service will work. */ | ||
public final class JpaPersistOptions { | ||
|
||
private final boolean autoBeginWorkOnEntityManagerCreation; | ||
|
||
private JpaPersistOptions(JpaPersistOptions.Builder builder) { | ||
this.autoBeginWorkOnEntityManagerCreation = builder.autoBeginWorkOnEntityManagerCreation; | ||
} | ||
|
||
/** | ||
* Returns true if the work unit should automatically begin when the EntityManager is created, if | ||
* it hasn't already begun. | ||
* | ||
* <p>This defaults to <b>false</b> because it's not safe, as careless usage can lead to leaking | ||
* sessions. | ||
*/ | ||
public boolean getAutoBeginWorkOnEntityManagerCreation() { | ||
return autoBeginWorkOnEntityManagerCreation; | ||
} | ||
|
||
/** Returns a builder to set options. */ | ||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
/** A builder to create the options. */ | ||
public static final class Builder { | ||
private boolean autoBeginWorkOnEntityManagerCreation; | ||
|
||
private Builder() {} | ||
|
||
public JpaPersistOptions build() { | ||
return new JpaPersistOptions(this); | ||
} | ||
|
||
/** Sets the {@link JpaPersistOptions#getAutoBeginWorkOnEntityManagerCreation} property. */ | ||
public Builder setAutoBeginWorkOnEntityManagerCreation( | ||
boolean autoBeginWorkOnEntityManagerCreation) { | ||
this.autoBeginWorkOnEntityManagerCreation = autoBeginWorkOnEntityManagerCreation; | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,24 +33,30 @@ | |
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
|
||
/** @author Dhanji R. Prasanna ([email protected]) */ | ||
/** | ||
* @author Dhanji R. Prasanna ([email protected]) | ||
*/ | ||
@Singleton | ||
class JpaPersistService implements Provider<EntityManager>, UnitOfWork, PersistService { | ||
private final ThreadLocal<EntityManager> entityManager = new ThreadLocal<>(); | ||
|
||
private final String persistenceUnitName; | ||
private final Map<?, ?> persistenceProperties; | ||
private final JpaPersistOptions options; | ||
|
||
@Inject | ||
public JpaPersistService( | ||
@Jpa String persistenceUnitName, @Nullable @Jpa Map<?, ?> persistenceProperties) { | ||
@Jpa JpaPersistOptions options, | ||
@Jpa String persistenceUnitName, | ||
@Nullable @Jpa Map<?, ?> persistenceProperties) { | ||
this.options = options; | ||
this.persistenceUnitName = persistenceUnitName; | ||
this.persistenceProperties = persistenceProperties; | ||
} | ||
|
||
@Override | ||
public EntityManager get() { | ||
if (!isWorking()) { | ||
if (options.getAutoBeginWorkOnEntityManagerCreation() && !isWorking()) { | ||
begin(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.