HHH-12921 - Hibernate Connection Pool Validation Thread should be defined as a daemon Thread

HHH-12922 - Hibernate Connection Pool Validation Thread should have a name
This commit is contained in:
Carlos Aristu 2018-08-22 14:24:49 +02:00 committed by Vlad Mihalcea
parent 9f1fde7127
commit 6911efdfb7
2 changed files with 79 additions and 7 deletions

View File

@ -14,6 +14,7 @@ import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -384,14 +385,9 @@ public class DriverManagerConnectionProviderImpl
if ( active ) {
return;
}
executorService = Executors.newSingleThreadScheduledExecutor();
executorService = Executors.newSingleThreadScheduledExecutor( new ValidationThreadFactory() );
executorService.scheduleWithFixedDelay(
new Runnable() {
@Override
public void run() {
pool.validate();
}
},
pool::validate,
validationInterval,
validationInterval,
TimeUnit.SECONDS
@ -453,4 +449,15 @@ public class DriverManagerConnectionProviderImpl
}
}
private static class ValidationThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread( runnable );
thread.setDaemon( true );
thread.setName( "Hibernate Connection Pool Validation Thread" );
return thread;
}
}
}

View File

@ -0,0 +1,65 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.connection;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertTrue;
/**
* @author Vlad Mihalcea
*/
public class DriverManagerConnectionProviderValidationConfigTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Event.class
};
}
@Override
protected void addConfigOptions(Map options) {
options.put( DriverManagerConnectionProviderImpl.VALIDATION_INTERVAL, 1L );
}
@Test
public void test() {
doInJPA( this::entityManagerFactory, entityManager -> {
Event event = new Event();
entityManager.persist( event );
assertTrue( Thread.getAllStackTraces()
.keySet()
.stream()
.filter( thread -> thread.getName()
.equals( "Hibernate Connection Pool Validation Thread" ) && thread.isDaemon() )
.map( Thread::isDaemon )
.findAny()
.isPresent() );
} );
}
@Entity(name = "Event")
public static class Event {
@Id
@GeneratedValue
private Long id;
private String name;
}
}