Browse Source

DataSourceUtils lets timeout exceptions through even for setReadOnly calls (revised; SPR-7226)

pull/1234/head
Juergen Hoeller 15 years ago
parent
commit
8800bab8a6
  1. 5
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java
  2. 28
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java

5
org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.jdbc.datasource; @@ -18,7 +18,6 @@ package org.springframework.jdbc.datasource;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean;
@ -236,7 +235,7 @@ public class DataSourceTransactionManager extends AbstractPlatformTransactionMan @@ -236,7 +235,7 @@ public class DataSourceTransactionManager extends AbstractPlatformTransactionMan
}
}
catch (SQLException ex) {
catch (Exception ex) {
DataSourceUtils.releaseConnection(con, this.dataSource);
throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
}

28
org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java

@ -154,14 +154,28 @@ public abstract class DataSourceUtils { @@ -154,14 +154,28 @@ public abstract class DataSourceUtils {
}
con.setReadOnly(true);
}
catch (Throwable ex) {
if (ex instanceof SQLException && (ex.getClass().getSimpleName().contains("Timeout") ||
(ex.getCause() != null && ex.getCause().getClass().getSimpleName().contains("Timeout")))) {
// Assume it's a connection timeout that would otherwise get lost: e.g. from C3P0.
throw (SQLException) ex;
catch (SQLException ex) {
Throwable exToCheck = ex;
while (exToCheck != null) {
if (exToCheck.getClass().getSimpleName().contains("Timeout")) {
// Assume it's a connection timeout that would otherwise get lost: e.g. from JDBC 4.0
throw ex;
}
exToCheck = exToCheck.getCause();
}
// "read-only not supported" SQLException or UnsupportedOperationException
// -> ignore, it's just a hint anyway.
// "read-only not supported" SQLException -> ignore, it's just a hint anyway
logger.debug("Could not set JDBC Connection read-only", ex);
}
catch (RuntimeException ex) {
Throwable exToCheck = ex;
while (exToCheck != null) {
if (exToCheck.getClass().getSimpleName().contains("Timeout")) {
// Assume it's a connection timeout that would otherwise get lost: e.g. from Hibernate
throw ex;
}
exToCheck = exToCheck.getCause();
}
// "read-only not supported" UnsupportedOperationException -> ignore, it's just a hint anyway
logger.debug("Could not set JDBC Connection read-only", ex);
}
}

Loading…
Cancel
Save