2011-06-17 30 views
1

Hallo。 因为似乎我不能在我的DAO使用Spring的DataAccessException的翻译机制,我想知道是否可以手动翻译Spring将java.sql.SQLException转换为DataAccessException

Internal Exception: java.sql.SQLException: [BEA][Oracle JDBC Driver][Oracle]ORA-00001: unique constraint (JSP_OWN.IDX_MC_CC_RAPPORTI_02) violated 

到DataAccessException体系。

亲切的问候 马西莫

回答

1

如果你有JdbcTemplate,你可以做

catch (SqlException e) { 
    throw jdbcTemplate.getExceptionTranslator().translate("my task", null, e); 
} 

如果没有JdbcTemplate做什么,只是看JdbcTemplate.getExceptionTranslator()方法的源代码:

public synchronized SQLExceptionTranslator getExceptionTranslator() { 
    if (this.exceptionTranslator == null) { 
     DataSource dataSource = getDataSource(); 
     if (dataSource != null) { 
      this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); 
     } 
     else { 
      this.exceptionTranslator = new SQLStateSQLExceptionTranslator(); 
     } 
    } 
    return this.exceptionTranslator; 
} 

并模仿它的行为:-)

相关问题