Skip to content

PreparedStatement#getParameterMetaData() calls may trigger unwanted side effects [SPR-11386] #16013

Closed
@spring-projects-issues

Description

@spring-projects-issues

Philippe Marschall opened SPR-11386 and commented

When upgrading from Spring 3.2.0 to Spring 4.0.0 we suddenly get the following exception.

Caused by: java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@47538759
	at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:154)
	at org.jboss.jca.adapters.jdbc.WrappedStatement.lock(WrappedStatement.java:114)
	at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setNull(WrappedPreparedStatement.java:539)
	at org.springframework.jdbc.core.StatementCreatorUtils.setNull(StatementCreatorUtils.java:285)
	at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:214)
	at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:148)
	at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.doSetValue(ArgumentPreparedStatementSetter.java:66)
	at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.setValues(ArgumentPreparedStatementSetter.java:47)
	at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:699)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:642)

The tracked the issue down to:

  1. StatementCreatorUtils now calls PreparedStatement#getParameterMetaData() for null bind parameters
  2. PreparedStatement#getParameterMetaData() throws an java.lang.ArrayIndexOutOfBoundsException if the SQL query contains a comment
  3. the JBoss connection pool (IronJacamar) disassociates the PreparedStatement with the connection
  4. StatementCreatorUtils swallows the exception and goes on to call PreparedStatement#setNull()
  5. the JBoss connection pool throws an exception because the PreparedStatement is no longer valid

The issue can be reproduced with the following code.

jdbcTemplate.queryForList("SELECT /* t */ 1 FROM dual where dummy = ?", Integer.class, new Object[] {null});

The quick fix we currently use is add "Oracle JDBC driver" to StatementCreatorUtils#driversWithNoSupportForGetParameterTypes through reflection but this is a hack at best.

Not using comments is not an option for us as we tag most of our queries with comments for monitoring in Enterprise Manager Cloud Control.

Not using null is not always easy as sometimes we have dynamic SQL where it's just easier to use NVL or IS NULL instead of several different queries.

We're aware that this is actually an Oracle bug and not a Spring bug but we need a better work around until we get a fix from Oracle. We're aware that JBoss actually certified the 12c driver.

Environment:

  • Java 7u45
  • ojdbc7 12.1.0.1
  • JBoss EAP 6.1.0

Affects: 3.2.6, 4.0 GA

Issue Links:

Referenced from: commits 8a6b095, 036bd79, 60b24cf, 6634c19, 60c1905

Backported to: 3.2.8

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions