@@ -87,24 +87,26 @@ public class EmbeddedPostgres implements Closeable {
8787 private final FileChannel lockChannel ;
8888 private final FileLock lock ;
8989 private final boolean cleanDataDirectory ;
90+ private final boolean registerShutdownHook ;
9091
9192 private final ProcessBuilder .Redirect errorRedirector ;
9293 private final ProcessBuilder .Redirect outputRedirector ;
9394
9495 @ SuppressWarnings ("unused" )
95- EmbeddedPostgres (final Path parentDirectory , final Path dataDirectory , final boolean cleanDataDirectory ,
96+ EmbeddedPostgres (final Path parentDirectory , final Path dataDirectory , final boolean cleanDataDirectory , boolean registerShutdownHook ,
9697 final Map <String , String > postgresConfig , final Map <String , String > localeConfig , final int port , final Map <String , String > connectConfig ,
9798 final PgBinaryResolver pgBinaryResolver , final ProcessBuilder .Redirect errorRedirector , final ProcessBuilder .Redirect outputRedirector ) throws IOException {
98- this (parentDirectory , dataDirectory , cleanDataDirectory , postgresConfig , localeConfig , port , connectConfig ,
99+ this (parentDirectory , dataDirectory , cleanDataDirectory , registerShutdownHook , postgresConfig , localeConfig , port , connectConfig ,
99100 pgBinaryResolver , errorRedirector , outputRedirector , DEFAULT_PG_STARTUP_WAIT , null , null );
100101 }
101102
102- EmbeddedPostgres (final Path parentDirectory , final Path dataDirectory , final boolean cleanDataDirectory ,
103+ EmbeddedPostgres (final Path parentDirectory , final Path dataDirectory , final boolean cleanDataDirectory , boolean registerShutdownHook ,
103104 final Map <String , String > postgresConfig , final Map <String , String > localeConfig , final int port , final Map <String , String > connectConfig ,
104105 final PgBinaryResolver pgBinaryResolver , final ProcessBuilder .Redirect errorRedirector ,
105106 final ProcessBuilder .Redirect outputRedirector , final Duration pgStartupWait ,
106107 final Path overrideWorkingDirectory , final Consumer <Path > dataDirectoryCustomizer ) throws IOException {
107108 this .cleanDataDirectory = cleanDataDirectory ;
109+ this .registerShutdownHook = registerShutdownHook ;
108110 this .postgresConfig = new HashMap <>(postgresConfig );
109111 this .localeConfig = new HashMap <>(localeConfig );
110112 this .connectConfig = new HashMap <>(connectConfig );
@@ -283,7 +285,7 @@ private void startPostmaster() {
283285 "-w" , "start"
284286 ));
285287
286- Runtime .getRuntime ().addShutdownHook (newCloserThread ());
288+ if ( registerShutdownHook ) Runtime .getRuntime ().addShutdownHook (newCloserThread ());
287289
288290 system (pgCtl , args , pgStartupWait );
289291
@@ -463,6 +465,7 @@ public static class Builder {
463465 private final Map <String , String > config = new HashMap <>();
464466 private final Map <String , String > localeConfig = new HashMap <>();
465467 private boolean builderCleanDataDirectory = true ;
468+ private boolean builderRegisterShutdownHook = true ;
466469 private int builderPort = 0 ;
467470 private final Map <String , String > connectConfig = new HashMap <>();
468471 private PgBinaryResolver pgBinaryResolver = DefaultPostgresBinaryResolver .INSTANCE ;
@@ -509,6 +512,20 @@ public Builder setCleanDataDirectory(final boolean cleanDataDirectory) {
509512 return this ;
510513 }
511514
515+ /**
516+ * Configures whether a JVM shutdown hook should be registered to
517+ * automatically close the embedded PostgreSQL instance on shutdown.
518+ *
519+ * @param registerShutdownHook {@code true} to register a shutdown hook,
520+ * {@code false} to disable it
521+ * @return this builder instance for chaining
522+ */
523+ @ SuppressWarnings ("unused" )
524+ public Builder setRegisterShutdownHook (final boolean registerShutdownHook ) {
525+ builderRegisterShutdownHook = registerShutdownHook ;
526+ return this ;
527+ }
528+
512529 /**
513530 * Sets a fixed data directory for PostgreSQL to use instead of a
514531 * randomly generated temporary directory.
@@ -653,7 +670,7 @@ public Builder setDataDirectoryCustomizer(final Consumer<Path> dataDirectoryCust
653670 public EmbeddedPostgres start () throws IOException {
654671 if (builderPort == 0 ) builderPort = detectFreePort ();
655672 if (builderDataDirectory == null ) builderDataDirectory = Files .createTempDirectory ("epg" );
656- return new EmbeddedPostgres (parentDirectory , builderDataDirectory , builderCleanDataDirectory , config ,
673+ return new EmbeddedPostgres (parentDirectory , builderDataDirectory , builderCleanDataDirectory , builderRegisterShutdownHook , config ,
657674 localeConfig , builderPort , connectConfig , pgBinaryResolver , errRedirector , outRedirector ,
658675 pgStartupWait , overrideWorkingDirectory , dataDirectoryCustomizer );
659676 }
@@ -673,6 +690,7 @@ public boolean equals(final Object o) {
673690 if (o == null || getClass () != o .getClass ()) return false ;
674691 final Builder builder = (Builder ) o ;
675692 return builderCleanDataDirectory == builder .builderCleanDataDirectory &&
693+ builderRegisterShutdownHook == builder .builderRegisterShutdownHook &&
676694 builderPort == builder .builderPort &&
677695 Objects .equals (parentDirectory , builder .parentDirectory ) &&
678696 Objects .equals (builderDataDirectory , builder .builderDataDirectory ) &&
@@ -689,7 +707,7 @@ public boolean equals(final Object o) {
689707
690708 @ Override
691709 public int hashCode () {
692- return Objects .hash (parentDirectory , builderDataDirectory , config , localeConfig , builderCleanDataDirectory , builderPort , connectConfig , pgBinaryResolver , pgStartupWait , errRedirector , outRedirector );
710+ return Objects .hash (parentDirectory , builderDataDirectory , config , localeConfig , builderCleanDataDirectory , builderRegisterShutdownHook , builderPort , connectConfig , pgBinaryResolver , pgStartupWait , errRedirector , outRedirector );
693711 }
694712 }
695713
@@ -768,6 +786,7 @@ private static String getArchitecture() {
768786 * @param stream A stream with the postgres binaries.
769787 * @param targetDir The directory to extract the content to.
770788 */
789+ @ SuppressWarnings ("java:S2612" )
771790 private static void extractTxz (final InputStream stream , final Path targetDir ) throws IOException {
772791 try (final XZInputStream xzIn = new XZInputStream (stream );
773792 final TarArchiveInputStream tarIn = new TarArchiveInputStream (xzIn )) {
@@ -845,7 +864,7 @@ private void closeChannel(final Channel channel) {
845864 }
846865 }
847866
848- @ SuppressWarnings ("java:S1141" )
867+ @ SuppressWarnings ({ "java:S1141" , "java:S2612" } )
849868 private static Path prepareBinaries (final PgBinaryResolver pgBinaryResolver , final Path overrideWorkingDirectory ) {
850869 PREPARE_BINARIES_LOCK .lock ();
851870 try {
0 commit comments