@Test public void badSqlGrammar() { SQLException sex = new SQLException("Message", "42001", 1); try { throw this.trans.translate("task", sql, sex); } catch (BadSqlGrammarException ex) { // OK assertTrue("SQL is correct", sql.equals(ex.getSql())); assertTrue("Exception matches", sex.equals(ex.getSQLException())); } }
@Test public void batchExceptionTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException badSqlEx = new SQLException("", "", 1); BatchUpdateException batchUpdateEx = new BatchUpdateException(); batchUpdateEx.setNextException(badSqlEx); BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx); assertEquals("SQL", bsgex.getSql()); assertEquals(badSqlEx, bsgex.getSQLException()); }
@Test public void errorCodeTranslation() { SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); SQLException badSqlEx = new SQLException("", "", 1); BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx); assertEquals("SQL", bsgex.getSql()); assertEquals(badSqlEx, bsgex.getSQLException()); SQLException invResEx = new SQLException("", "", 4); InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx); assertEquals("SQL", irsex.getSql()); assertEquals(invResEx, irsex.getSQLException()); checkTranslation(sext, 5, DataAccessResourceFailureException.class); checkTranslation(sext, 6, DataIntegrityViolationException.class); checkTranslation(sext, 7, CannotAcquireLockException.class); checkTranslation(sext, 8, DeadlockLoserDataAccessException.class); checkTranslation(sext, 9, CannotSerializeTransactionException.class); checkTranslation(sext, 10, DuplicateKeyException.class); SQLException dupKeyEx = new SQLException("", "", 10); DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx); assertTrue("Not instance of DataIntegrityViolationException", DataIntegrityViolationException.class.isAssignableFrom(dksex.getClass())); // Test fallback. We assume that no database will ever return this error code, // but 07xxx will be bad grammar picked up by the fallback SQLState translator SQLException sex = new SQLException("", "07xxx", 666666666); BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", sex); assertEquals("SQL2", bsgex2.getSql()); assertEquals(sex, bsgex2.getSQLException()); }
BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2); assertEquals("SQL2", bsgex2.getSql()); assertEquals(badSqlEx2, bsgex2.getSQLException()); BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct); assertEquals("SQL-ECT", bsgEct.getSql()); assertEquals(sexEct, bsgEct.getSQLException()); BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt); assertEquals("SQL-FBT", bsgFbt.getSql()); assertEquals(sexFbt, bsgFbt.getSQLException());
private void createTable0(int retry, IJdbcTemplateCallback callback, Object... args){ try { createTable(getTableName()); }catch(BadSqlGrammarException e){ if(e.getSQLException() == null || !e.getSQLException().getMessage().contains("already exists")) throw e; if(!save0(++retry, callback, args)) throw e; } }
private boolean save0(int retry, IJdbcTemplateCallback callback, Object... args){ // 只允许重试三次 if(retry > 2) return false; try { callback.handle(getJdbcTemplate(), args); return true; }catch(BadSqlGrammarException e){ if(e.getSQLException() == null || !e.getSQLException().getMessage().contains("doesn't exist")) throw e; ++retry; createTable0(retry, callback, args); return save0(retry, callback, args); } }
System.out.println("====> " + e.getMessage()); if (e instanceof BadSqlGrammarException) { String msg = ((BadSqlGrammarException) e).getSQLException().getMessage(); if (msg.indexOf("because it does not exist") > -1) { continue;
new UserObjectRowMapper()); } catch (BadSqlGrammarException sqlGrammarException) { if (sqlGrammarException.getSQLException().getErrorCode() == 942) { String userName = null; try {