diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java index 804a9b29d8..c49e118db8 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java @@ -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; + } + } + } diff --git a/hibernate-core/src/test/java/org/hibernate/connection/DriverManagerConnectionProviderValidationConfigTest.java b/hibernate-core/src/test/java/org/hibernate/connection/DriverManagerConnectionProviderValidationConfigTest.java new file mode 100644 index 0000000000..b33b527edb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/connection/DriverManagerConnectionProviderValidationConfigTest.java @@ -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 . + */ +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; + } +} \ No newline at end of file