{"record":{"id":"4891eff4ef12d07c","repo":"hibernate/hibernate-orm","slug":"cannot-simultaneously-share-transaction-context-an","errorCode":null,"errorMessage":"Cannot simultaneously share transaction context and specify connection","messagePattern":"Cannot simultaneously share transaction context and specify connection","errorType":"exception","errorClass":"SessionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java","lineNumber":312,"sourceCode":"\t\tinterceptor = interpret( options.resolveInterceptor( factory ) );\n\t\tjdbcTimeZone = options.getJdbcTimeZone();\n\n\t\tsessionEventsManager = createSessionEventsManager( factoryOptions, options );\n\t\tentityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor, this );\n\n\t\tcriteriaCopyTreeEnabled = factoryOptions.isCriteriaCopyTreeEnabled();\n\t\tcriteriaPlanCacheEnabled = factoryOptions.isCriteriaPlanCacheEnabled();\n\t\tnativeJdbcParametersIgnored = factoryOptions.getNativeJdbcParametersIgnored();\n\n\t\tfinal var statementInspector = interpret( options.getStatementInspector() );\n\n\t\tchangesetIdSupplier = initializeChangesetIdSupplier( factory );\n\n\t\tif ( options instanceof SharedSessionCreationOptions sharedOptions\n\t\t\t\t&& sharedOptions.isTransactionCoordinatorShared() ) {\n\t\t\tisTransactionCoordinatorShared = true;\n\t\t\tif ( options.getConnection() != null ) {\n\t\t\t\tthrow new SessionException( \"Cannot simultaneously share transaction context and specify connection\" );\n\t\t\t}\n\n\t\t\tfinal var transactionCoordinator = sharedOptions.getTransactionCoordinator();\n\t\t\tfinal var jdbcCoordinator = sharedOptions.getJdbcCoordinator();\n\t\t\tassert transactionCoordinator != null;\n\t\t\tassert jdbcCoordinator != null;\n\t\t\tthis.transactionCoordinator = transactionCoordinator;\n\t\t\tthis.jdbcCoordinator = jdbcCoordinator;\n\n\t\t\t// todo : \"wrap\" the transaction to no-op commit/rollback attempts?\n\t\t\tcurrentHibernateTransaction = sharedOptions.getTransaction();\n\t\t\tconnectionHandlingMode = this.jdbcCoordinator.getLogicalConnection().getConnectionHandlingMode();\n\t\t\tautoJoinTransactions = false;\n\t\t\tjdbcSessionContext = createJdbcSessionContext( options.getStatementObserver(), statementInspector );\n\t\t\tlogInconsistentOptions( sharedOptions );\n\t\t\taddSharedSessionTransactionObserver( this.transactionCoordinator );\n\t\t\tsharedOptions.registerParentSessionObserver( new ParentSessionObserver() {\n\t\t\t\t@Override","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java#L294-L330","documentation":"Error \"Cannot simultaneously share transaction context and specify connection\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown when a session is built both sharing a transaction context and supplying an explicit connection.","commonSituations":"Typical situations: combining SessionBuilder.connection() with shared transaction-context options; conflicting session open options.","solutions":["Either share the transaction context or pass an explicit Connection when building the session — not both."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}