Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up database connection pool (#234)
Set up database connection pool
- Loading branch information
Showing
8 changed files
with
233 additions
and
23 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
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
67 changes: 67 additions & 0 deletions
67
core/src/main/java/google/registry/persistence/EntityManagerFactoryProvider.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,67 @@ | |||
// Copyright 2019 The Nomulus Authors. All Rights Reserved. | |||
// | |||
// 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 google.registry.persistence; | |||
|
|||
import static google.registry.config.RegistryConfig.getHibernateConnectionIsolation; | |||
import static google.registry.config.RegistryConfig.getHibernateHikariConnectionTimeout; | |||
import static google.registry.config.RegistryConfig.getHibernateHikariIdleTimeout; | |||
import static google.registry.config.RegistryConfig.getHibernateHikariMaximumPoolSize; | |||
import static google.registry.config.RegistryConfig.getHibernateHikariMinimumIdle; | |||
import static google.registry.config.RegistryConfig.getHibernateLogSqlQueries; | |||
|
|||
import com.google.common.collect.ImmutableMap; | |||
import javax.persistence.EntityManagerFactory; | |||
import javax.persistence.Persistence; | |||
import org.hibernate.cfg.Environment; | |||
|
|||
/** Factory class to provide {@link EntityManagerFactory} instance. */ | |||
public class EntityManagerFactoryProvider { | |||
// This name must be the same as the one defined in persistence.xml. | |||
public static final String PERSISTENCE_UNIT_NAME = "nomulus"; | |||
public static final String HIKARI_CONNECTION_TIMEOUT = "hibernate.hikari.connectionTimeout"; | |||
public static final String HIKARI_MINIMUM_IDLE = "hibernate.hikari.minimumIdle"; | |||
public static final String HIKARI_MAXIMUM_POOL_SIZE = "hibernate.hikari.maximumPoolSize"; | |||
public static final String HIKARI_IDLE_TIMEOUT = "hibernate.hikari.idleTimeout"; | |||
|
|||
private static ImmutableMap<String, String> getDefaultProperties() { | |||
ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); | |||
|
|||
properties.put(Environment.DRIVER, "org.postgresql.Driver"); | |||
properties.put( | |||
Environment.CONNECTION_PROVIDER, | |||
"org.hibernate.hikaricp.internal.HikariCPConnectionProvider"); | |||
// Whether to automatically validate and export schema DDL to the database when the | |||
// SessionFactory is created. Setting it to 'none' to turn off the feature. | |||
properties.put(Environment.HBM2DDL_AUTO, "none"); | |||
|
|||
properties.put(Environment.ISOLATION, getHibernateConnectionIsolation()); | |||
properties.put(Environment.SHOW_SQL, getHibernateLogSqlQueries()); | |||
properties.put(HIKARI_CONNECTION_TIMEOUT, getHibernateHikariConnectionTimeout()); | |||
properties.put(HIKARI_MINIMUM_IDLE, getHibernateHikariMinimumIdle()); | |||
properties.put(HIKARI_MAXIMUM_POOL_SIZE, getHibernateHikariMaximumPoolSize()); | |||
properties.put(HIKARI_IDLE_TIMEOUT, getHibernateHikariIdleTimeout()); | |||
return properties.build(); | |||
} | |||
|
|||
/** Constructs the {@link EntityManagerFactory} instance. */ | |||
public static EntityManagerFactory create(String jdbcUrl, String username, String password) { | |||
ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); | |||
properties.putAll(getDefaultProperties()); | |||
properties.put(Environment.URL, jdbcUrl); | |||
properties.put(Environment.USER, username); | |||
properties.put(Environment.PASS, password); | |||
return Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, properties.build()); | |||
} | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,28 @@ | |||
<?xml version="1.0" encoding="UTF-8" ?> | |||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence | |||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" | |||
version="2.2"> | |||
<persistence-unit name="nomulus" transaction-type="RESOURCE_LOCAL"> | |||
<description> | |||
Persistence unit for the Nomulus Cloud SQL database. | |||
</description> | |||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |||
|
|||
<!-- | |||
All JPA entities must be enumerated here. JPA does not support auto detection. | |||
Note that Hibernate's auto detection functionality (hibernate.archive.autodection) | |||
does not meet our needs. It only scans archives, not the 'classes' folders. So we | |||
are left with two options: | |||
* Move tests to another (sub)project. This is not a big problem, but feels unnatural. | |||
* Use Hibernate's ServiceRegistry for bootstrapping (not JPA-compliant) | |||
--> | |||
<class>google.registry.model.domain.DomainBase</class> | |||
<class>google.registry.schema.tmch.ClaimsList</class> | |||
|
|||
<!-- TODO(weiminyu): check out application-layer validation. --> | |||
<validation-mode>NONE</validation-mode> | |||
</persistence-unit> | |||
</persistence> |
55 changes: 55 additions & 0 deletions
55
core/src/test/java/google/registry/persistence/EntityManagerFactoryProviderTest.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,55 @@ | |||
// Copyright 2019 The Nomulus Authors. All Rights Reserved. | |||
// | |||
// 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 google.registry.persistence; | |||
|
|||
import static com.google.common.truth.Truth.assertThat; | |||
|
|||
import javax.persistence.EntityManager; | |||
import javax.persistence.EntityManagerFactory; | |||
import org.junit.After; | |||
import org.junit.Before; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.junit.runners.JUnit4; | |||
import org.testcontainers.containers.PostgreSQLContainer; | |||
|
|||
/** Unit tests for {@link EntityManagerFactoryProvider}. */ | |||
@RunWith(JUnit4.class) | |||
public class EntityManagerFactoryProviderTest { | |||
@Rule public PostgreSQLContainer database = new PostgreSQLContainer(); | |||
|
|||
private EntityManagerFactory emf; | |||
|
|||
@Before | |||
public void init() { | |||
emf = | |||
EntityManagerFactoryProvider.create( | |||
database.getJdbcUrl(), database.getUsername(), database.getPassword()); | |||
} | |||
|
|||
@After | |||
public void destroy() { | |||
emf.close(); | |||
emf = null; | |||
} | |||
|
|||
@Test | |||
public void testConnectToDatabase_success() { | |||
EntityManager em = emf.createEntityManager(); | |||
assertThat(em.isOpen()).isTrue(); | |||
em.close(); | |||
} | |||
} |
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