@@ -471,48 +471,56 @@ private boolean hasTargetConnection() {
471471 /**
472472 * Return the target Connection, fetching it and initializing it if necessary.
473473 */
474- private Connection getTargetConnection (Method operation ) throws SQLException {
475- if (this .target == null ) {
476- // No target Connection held -> fetch one.
474+ private Connection getTargetConnection (Method operation ) throws Throwable {
475+ Connection target = this .target ;
476+ if (target != null ) {
477+ // Target Connection already held -> return it.
477478 if (logger .isTraceEnabled ()) {
478- logger .trace ("Connecting to database for operation '" + operation .getName () + "'" );
479+ logger .trace ("Using existing database connection for operation '" + operation .getName () + "'" );
479480 }
481+ return target ;
482+ }
480483
481- // Fetch physical Connection from DataSource.
482- DataSource dataSource = getDataSourceToUse ();
483- this .target = (this .username != null ? dataSource .getConnection (this .username , this .password ) :
484- dataSource .getConnection ());
485- if (this .target == null ) {
486- throw new IllegalStateException ("DataSource returned null from getConnection(): " + dataSource );
487- }
484+ // No target Connection held -> fetch one.
485+ if (logger .isTraceEnabled ()) {
486+ logger .trace ("Connecting to database for operation '" + operation .getName () + "'" );
487+ }
488+
489+ // Fetch physical Connection from DataSource.
490+ DataSource dataSource = getDataSourceToUse ();
491+ target = (this .username != null ? dataSource .getConnection (this .username , this .password ) :
492+ dataSource .getConnection ());
493+ if (target == null ) {
494+ throw new IllegalStateException ("DataSource returned null from getConnection(): " + dataSource );
495+ }
488496
489- // Apply kept transaction settings, if any.
497+ // Apply kept transaction settings, if any.
498+ try {
490499 if (this .readOnly && readOnlyDataSource == null ) {
491- try {
492- this .target .setReadOnly (true );
493- }
494- catch (Exception ex ) {
495- // "read-only not supported" -> ignore, it's just a hint anyway
496- logger .debug ("Could not set JDBC Connection read-only" , ex );
497- }
500+ DataSourceUtils .setReadOnlyIfPossible (target );
498501 }
499502 if (this .transactionIsolation != null &&
500503 !this .transactionIsolation .equals (defaultTransactionIsolation ())) {
501- this . target .setTransactionIsolation (this .transactionIsolation );
504+ target .setTransactionIsolation (this .transactionIsolation );
502505 }
503506 if (this .autoCommit != null && this .autoCommit != defaultAutoCommit ()) {
504- this . target .setAutoCommit (this .autoCommit );
507+ target .setAutoCommit (this .autoCommit );
505508 }
506509 }
507-
508- else {
509- // Target Connection already held -> return it.
510- if (logger .isTraceEnabled ()) {
511- logger .trace ("Using existing database connection for operation '" + operation .getName () + "'" );
510+ catch (Throwable settingsEx ) {
511+ logger .debug ("Failed to apply transaction settings to JDBC Connection" , settingsEx );
512+ // Close Connection and do not set it as target.
513+ try {
514+ target .close ();
515+ }
516+ catch (Throwable closeEx ) {
517+ logger .debug ("Could not close JDBC Connection after failed settings" , closeEx );
512518 }
519+ throw settingsEx ;
513520 }
514521
515- return this .target ;
522+ this .target = target ;
523+ return target ;
516524 }
517525
518526 private DataSource getDataSourceToUse () {
0 commit comments