{"record":{"id":"9cfbc05543eea379","repo":"hibernate/hibernate-orm","slug":"synchronization-to-register-cannot-be-null","errorCode":null,"errorMessage":"Synchronization to register cannot be null","messagePattern":"Synchronization to register cannot be null","errorType":"exception","errorClass":"NullSynchronizationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/resource/transaction/internal/SynchronizationRegistryStandardImpl.java","lineNumber":37,"sourceCode":" * @author Steve Ebersole\n */\npublic class SynchronizationRegistryStandardImpl implements SynchronizationRegistryImplementor {\n\n\tprivate LinkedHashSet<Synchronization> synchronizations;\n\n\t/**\n\t * Intended for test access\n\t *\n\t * @return The number of Synchronizations registered\n\t */\n\tpublic int getNumberOfRegisteredSynchronizations() {\n\t\treturn synchronizations == null ? 0 : synchronizations.size();\n\t}\n\n\t@Override\n\tpublic void registerSynchronization(Synchronization synchronization) {\n\t\tif ( synchronization == null ) {\n\t\t\tthrow new NullSynchronizationException();\n\t\t}\n\n\t\tif ( synchronizations == null ) {\n\t\t\tsynchronizations = new LinkedHashSet<>();\n\t\t}\n\n\t\tfinal boolean added = synchronizations.add( synchronization );\n\t\tif ( !added ) {\n\t\t\tSYNCHRONIZATION_LOGGER.synchronizationAlreadyRegistered( synchronization );\n\t\t}\n\t}\n\n\t@Override\n\tpublic void notifySynchronizationsBeforeTransactionCompletion() {\n\t\tSYNCHRONIZATION_LOGGER.notifyingSynchronizationsBefore();\n\t\tif ( synchronizations != null ) {\n\t\t\tfor ( var synchronization : synchronizations ) {\n\t\t\t\ttry {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/resource/transaction/internal/SynchronizationRegistryStandardImpl.java#L19-L55","documentation":"SynchronizationRegistryStandardImpl.registerSynchronization(Synchronization) explicitly rejects a null argument by throwing NullSynchronizationException (\"Synchronization to register cannot be null\"), described in Hibernate as a glorified NullPointerException. It is a fail-fast guard on the transaction SPI: a null JTA Synchronization can never be notified, so registration is refused immediately.","triggerScenarios":"Calling Transaction.registerSynchronization(null), or passing the result of a supplier/factory method that returned null — e.g. a conditional listener builder whose branch returns null, or dependency injection that failed to produce the Synchronization bean.","commonSituations":"Custom integrators/interceptors registering listeners only in some environments; Spring or CDI producing a null bean for a Synchronization; code copied from tests where the callback was optional.","solutions":["Null-check the Synchronization before calling registerSynchronization","Fix the producer/factory that returns null so it always returns a real Synchronization","Only register synchronizations after the transaction has started, from code that can construct the callback reliably"],"exampleFix":"// before\ntransaction.registerSynchronization(maybeCreateListener()); // may return null\n\n// after\nfinal var listener = maybeCreateListener();\nif (listener != null) {\n    transaction.registerSynchronization(listener);\n}","handlingStrategy":"validation","validationCode":"final Synchronization sync = listenerFactory.create();\nif (sync != null) {\n    transaction.registerSynchronization(sync);\n}\nelse {\n    LOG.debug(\"no synchronization produced; skipping registration\");\n}","typeGuard":"static Synchronization orNull(Synchronization s) { return s; }\n// simply: guard with `s != null` before registerSynchronization","tryCatchPattern":"try {\n    transaction.registerSynchronization(sync);\n}\ncatch (NullSynchronizationException e) {\n    // programming bug: producer returned null; fix the producer\n    throw new IllegalStateException(\"listener factory returned null synchronization\", e);\n}","preventionTips":["Null-check every value produced by factories/DI before registering it","Keep Synchronization construction in one place so null returns are impossible to miss","Add a unit test asserting the factory never returns null"],"tags":["jta","synchronization","null-safety","transactions"],"backgroundTag":"null-argument","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}