@@ -460,48 +460,56 @@ private boolean hasTargetConnection() {
460460 /**
461461 * Return the target Connection, fetching it and initializing it if necessary.
462462 */
463- private Connection getTargetConnection (Method operation ) throws SQLException {
464- if (this .target == null ) {
465- // No target Connection held -> fetch one.
463+ private Connection getTargetConnection (Method operation ) throws Throwable {
464+ Connection target = this .target ;
465+ if (target != null ) {
466+ // Target Connection already held -> return it.
466467 if (logger .isTraceEnabled ()) {
467- logger .trace ("Connecting to database for operation '" + operation .getName () + "'" );
468+ logger .trace ("Using existing database connection for operation '" + operation .getName () + "'" );
468469 }
470+ return target ;
471+ }
469472
470- // Fetch physical Connection from DataSource.
471- DataSource dataSource = getDataSourceToUse ();
472- this .target = (this .username != null ? dataSource .getConnection (this .username , this .password ) :
473- dataSource .getConnection ());
474- if (this .target == null ) {
475- throw new IllegalStateException ("DataSource returned null from getConnection(): " + dataSource );
476- }
473+ // No target Connection held -> fetch one.
474+ if (logger .isTraceEnabled ()) {
475+ logger .trace ("Connecting to database for operation '" + operation .getName () + "'" );
476+ }
477+
478+ // Fetch physical Connection from DataSource.
479+ DataSource dataSource = getDataSourceToUse ();
480+ target = (this .username != null ? dataSource .getConnection (this .username , this .password ) :
481+ dataSource .getConnection ());
482+ if (target == null ) {
483+ throw new IllegalStateException ("DataSource returned null from getConnection(): " + dataSource );
484+ }
477485
478- // Apply kept transaction settings, if any.
486+ // Apply kept transaction settings, if any.
487+ try {
479488 if (this .readOnly && readOnlyDataSource == null ) {
480- try {
481- this .target .setReadOnly (true );
482- }
483- catch (Exception ex ) {
484- // "read-only not supported" -> ignore, it's just a hint anyway
485- logger .debug ("Could not set JDBC Connection read-only" , ex );
486- }
489+ DataSourceUtils .setReadOnlyIfPossible (target );
487490 }
488491 if (this .transactionIsolation != null &&
489492 !this .transactionIsolation .equals (defaultTransactionIsolation ())) {
490- this . target .setTransactionIsolation (this .transactionIsolation );
493+ target .setTransactionIsolation (this .transactionIsolation );
491494 }
492495 if (this .autoCommit != null && this .autoCommit != defaultAutoCommit ()) {
493- this . target .setAutoCommit (this .autoCommit );
496+ target .setAutoCommit (this .autoCommit );
494497 }
495498 }
496-
497- else {
498- // Target Connection already held -> return it.
499- if (logger .isTraceEnabled ()) {
500- logger .trace ("Using existing database connection for operation '" + operation .getName () + "'" );
499+ catch (Throwable settingsEx ) {
500+ logger .debug ("Failed to apply transaction settings to JDBC Connection" , settingsEx );
501+ // Close Connection and do not set it as target.
502+ try {
503+ target .close ();
504+ }
505+ catch (Throwable closeEx ) {
506+ logger .debug ("Could not close JDBC Connection after failed settings" , closeEx );
501507 }
508+ throw settingsEx ;
502509 }
503510
504- return this .target ;
511+ this .target = target ;
512+ return target ;
505513 }
506514
507515 private DataSource getDataSourceToUse () {
0 commit comments