List of usage examples for java.sql SQLException getSQLState
public String getSQLState()
SQLException
object. From source file:com.feedzai.commons.sql.abstraction.engine.impl.H2Engine.java
@Override protected void addFks(DbEntity entity) throws DatabaseEngineException { for (DbFk fk : entity.getFks()) { final List<String> quotizedLocalColumns = new ArrayList<String>(); for (String s : fk.getLocalColumns()) { quotizedLocalColumns.add(quotize(s)); }//from www.j a v a 2 s . co m final List<String> quotizedForeignColumns = new ArrayList<String>(); for (String s : fk.getForeignColumns()) { quotizedForeignColumns.add(quotize(s)); } final String table = quotize(entity.getName()); final String quotizedLocalColumnsSting = join(quotizedLocalColumns, ", "); final String quotizedForeignColumnsString = join(quotizedForeignColumns, ", "); final String alterTable = format("ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)", table, quotize(md5("FK_" + table + quotizedLocalColumnsSting + quotizedForeignColumnsString, properties.getMaxIdentifierSize())), quotizedLocalColumnsSting, quotize(fk.getForeignTable()), quotizedForeignColumnsString); Statement alterTableStmt = null; try { alterTableStmt = conn.createStatement(); logger.trace(alterTable); alterTableStmt.executeUpdate(alterTable); } catch (SQLException ex) { if (ex.getSQLState().equals(CONSTRAINT_NAME_ALREADY_EXISTS)) { logger.debug(dev, "Foreign key for table '{}' already exists. Error code: {}.", entity.getName(), ex.getSQLState()); } else { throw new DatabaseEngineException( format("Could not add Foreign Key to entity %s. Error code: %s.", entity.getName(), ex.getSQLState()), ex); } } finally { try { if (alterTableStmt != null) { alterTableStmt.close(); } } catch (Exception e) { logger.trace("Error closing statement.", e); } } } }
From source file:com.feedzai.commons.sql.abstraction.engine.impl.H2Engine.java
@Override protected void addIndexes(final DbEntity entity) throws DatabaseEngineException { List<DbIndex> indexes = entity.getIndexes(); for (DbIndex index : indexes) { List<String> createIndex = new ArrayList<String>(); createIndex.add("CREATE"); if (index.isUnique()) { createIndex.add("UNIQUE"); }//from w w w. j a v a 2s . c o m createIndex.add("INDEX"); List<String> columns = new ArrayList<String>(); List<String> columnsForName = new ArrayList<String>(); for (String column : index.getColumns()) { columns.add(quotize(column)); columnsForName.add(column); } final String idxName = md5(format("%s_%s_IDX", entity.getName(), join(columnsForName, "_")), properties.getMaxIdentifierSize()); createIndex.add(quotize(idxName)); createIndex.add("ON"); createIndex.add(quotize(entity.getName())); createIndex.add("(" + join(columns, ", ") + ")"); final String statement = join(createIndex, " "); logger.trace(statement); Statement s = null; try { s = conn.createStatement(); s.executeUpdate(statement); } catch (SQLException ex) { if (ex.getSQLState().startsWith(INDEX_ALREADY_EXISTS)) { logger.debug(dev, "'{}' is already defined", idxName); handleOperation(new OperationFault(entity.getName(), OperationFault.Type.INDEX_ALREADY_EXISTS), ex); } else { throw new DatabaseEngineException("Something went wrong handling statement", ex); } } finally { try { if (s != null) { s.close(); } } catch (Exception e) { logger.trace("Error closing statement.", e); } } } }
From source file:org.apache.openjpa.jdbc.sql.MySQLDictionary.java
@Override protected int matchErrorState(Map<Integer, Set<String>> errorStates, SQLException ex) { int state = super.matchErrorState(errorStates, ex); // OPENJPA-1616 - Special case for MySQL not returning a SQLState for timeouts if (state == StoreException.GENERAL && ex.getErrorCode() == 0 && ex.getSQLState() == null) { // look at the nested MySQL exception for more details SQLException sqle = ex.getNextException(); if (sqle != null && sqle.toString().startsWith("com.mysql.jdbc.exceptions.MySQLTimeoutException")) { if (conf != null && conf.getLockTimeout() != -1) { state = StoreException.LOCK; } else { state = StoreException.QUERY; }/*from w w w. j av a2 s. c o m*/ } } return state; }
From source file:com.feedzai.commons.sql.abstraction.engine.impl.H2Engine.java
@Override protected void createTable(DbEntity entity) throws DatabaseEngineException { entity = injectNotNullIfMissing(entity); List<String> createTable = new ArrayList<String>(); createTable.add("CREATE TABLE"); createTable.add(quotize(entity.getName())); List<String> columns = new ArrayList<String>(); List<String> pkFields = entity.getPkFields(); for (DbColumn c : entity.getColumns()) { List<String> column = new ArrayList<String>(); column.add(quotize(c.getName())); column.add(translateType(c));/*www .j a v a 2 s. co m*/ // If this column is PK, it must be forced to be NOT NULL (only if it's not already...) if (pkFields.contains(c.getName()) && !c.getColumnConstraints().contains(DbColumnConstraint.NOT_NULL)) { // Create a NOT NULL constraint c.getColumnConstraints().add(DbColumnConstraint.NOT_NULL); } for (DbColumnConstraint cc : c.getColumnConstraints()) { column.add(cc.translate()); } if (c.isDefaultValueSet()) { column.add("DEFAULT"); column.add(translate(c.getDefaultValue())); } columns.add(join(column, " ")); } createTable.add("(" + join(columns, ", ") + ")"); final String createTableStatement = join(createTable, " "); logger.trace(createTableStatement); Statement s = null; try { s = conn.createStatement(); s.executeUpdate(createTableStatement); } catch (SQLException ex) { if (ex.getSQLState().startsWith(NAME_ALREADY_EXISTS)) { logger.debug(dev, "'{}' is already defined", entity.getName()); handleOperation(new OperationFault(entity.getName(), OperationFault.Type.TABLE_ALREADY_EXISTS), ex); } else { throw new DatabaseEngineException("Something went wrong handling statement", ex); } } finally { try { if (s != null) { s.close(); } } catch (Exception e) { logger.trace("Error closing statement.", e); } } }
From source file:com.feedzai.commons.sql.abstraction.engine.impl.PostgreSqlEngine.java
@Override protected void addPrimaryKey(final DbEntity entity) throws DatabaseEngineException { if (entity.getPkFields().size() == 0) { return;// w ww . j av a 2s . c o m } List<String> pks = new ArrayList<String>(); for (String pk : entity.getPkFields()) { pks.add(quotize(pk)); } final String pkName = md5(format("PK_%s", entity.getName()), properties.getMaxIdentifierSize()); List<String> statement = new ArrayList<String>(); statement.add("ALTER TABLE"); statement.add(quotize(entity.getName())); statement.add("ADD CONSTRAINT"); statement.add(quotize(pkName)); statement.add("PRIMARY KEY"); statement.add("(" + join(pks, ", ") + ")"); final String addPrimaryKey = join(statement, " "); logger.trace(addPrimaryKey); Statement s = null; try { s = conn.createStatement(); s.executeUpdate(addPrimaryKey); } catch (SQLException ex) { if (ex.getSQLState().startsWith(TABLE_CAN_ONLY_HAVE_ONE_PRIMARY_KEY)) { logger.debug(dev, "'{}' already has a primary key", entity.getName()); handleOperation( new OperationFault(entity.getName(), OperationFault.Type.PRIMARY_KEY_ALREADY_EXISTS), ex); } else { throw new DatabaseEngineException("Something went wrong handling statement", ex); } } finally { try { if (s != null) { s.close(); } } catch (Exception e) { logger.trace("Error closing statement.", e); } } }
From source file:org.apache.openjpa.jdbc.sql.MySQLDictionary.java
@Override public boolean isFatalException(int subtype, SQLException ex) { if ((subtype == StoreException.LOCK && ex.getErrorCode() == 1205) || (subtype == StoreException.QUERY && ex.getErrorCode() == 1317)) { return false; }// w w w. j av a 2s .co m if (ex.getErrorCode() == 0 && ex.getSQLState() == null) return false; return super.isFatalException(subtype, ex); }
From source file:com.feedzai.commons.sql.abstraction.engine.impl.PostgreSqlEngine.java
@Override protected void createTable(final DbEntity entity) throws DatabaseEngineException { List<String> createTable = new ArrayList<String>(); createTable.add("CREATE TABLE"); createTable.add(quotize(entity.getName())); List<String> columns = new ArrayList<String>(); for (DbColumn c : entity.getColumns()) { List<String> column = new ArrayList<String>(); column.add(quotize(c.getName())); column.add(translateType(c));//from www. j a va 2 s. c o m for (DbColumnConstraint cc : c.getColumnConstraints()) { column.add(cc.translate()); } if (c.isDefaultValueSet()) { column.add("DEFAULT"); column.add(translate(c.getDefaultValue())); } columns.add(join(column, " ")); } createTable.add("(" + join(columns, ", ") + ")"); final String createTableStatement = join(createTable, " "); logger.trace(createTableStatement); Statement s = null; try { s = conn.createStatement(); s.executeUpdate(createTableStatement); } catch (SQLException ex) { if (ex.getSQLState().startsWith(NAME_ALREADY_EXISTS)) { logger.debug(dev, "'{}' is already defined", entity.getName()); handleOperation(new OperationFault(entity.getName(), OperationFault.Type.TABLE_ALREADY_EXISTS), ex); } else { throw new DatabaseEngineException("Something went wrong handling statement", ex); } } finally { try { if (s != null) { s.close(); } } catch (Exception e) { logger.trace("Error closing statement.", e); } } }
From source file:com.feedzai.commons.sql.abstraction.engine.impl.PostgreSqlEngine.java
@Override protected void addIndexes(final DbEntity entity) throws DatabaseEngineException { List<DbIndex> indexes = entity.getIndexes(); for (DbIndex index : indexes) { List<String> createIndex = new ArrayList<String>(); createIndex.add("CREATE"); if (index.isUnique()) { createIndex.add("UNIQUE"); }/*from w w w .j av a2 s . c o m*/ createIndex.add("INDEX"); List<String> columns = new ArrayList<String>(); List<String> columnsForName = new ArrayList<String>(); for (String column : index.getColumns()) { columns.add(quotize(column)); columnsForName.add(column); } final String idxName = md5(format("%s_%s_IDX", entity.getName(), join(columnsForName, "_")), properties.getMaxIdentifierSize()); createIndex.add(quotize(idxName)); createIndex.add("ON"); createIndex.add(quotize(entity.getName())); createIndex.add("(" + join(columns, ", ") + ")"); final String statement = join(createIndex, " "); logger.trace(statement); Statement s = null; try { s = conn.createStatement(); s.executeUpdate(statement); } catch (SQLException ex) { if (ex.getSQLState().startsWith(NAME_ALREADY_EXISTS)) { logger.debug(dev, "'{}' is already defined", idxName); handleOperation(new OperationFault(entity.getName(), OperationFault.Type.INDEX_ALREADY_EXISTS), ex); } else { throw new DatabaseEngineException("Something went wrong handling statement", ex); } } finally { try { if (s != null) { s.close(); } } catch (Exception e) { logger.trace("Error closing statement.", e); } } } }
From source file:org.tranql.connector.jdbc.PreparedStatementWrapper.java
protected void closeStatement() { try {//from w w w . ja v a 2s . c o m if (!statementClosed) { ps.close(); statementClosed = true; } } catch (SQLException e) { log.error("TRANQL DB2 EmbeddedXA Driver. Error closing PreparedStatement in closeStatement.\n" + " Error message = " + e.getMessage() + "\n" + " Error code = " + Integer.toString(e.getErrorCode()) + " SQLState = " + e.getSQLState()); e.printStackTrace(); } }
From source file:org.nuclos.server.dblayer.impl.oracle.OracleDBAccess.java
@Override protected DbException wrapSQLException(Long id, String message, SQLException ex) { try {/* w w w.j av a2 s . co m*/ if ("23000".equals(ex.getSQLState())) { Matcher matcher; switch (ex.getErrorCode()) { case 1: // unique constraint matcher = EXCEPTION_IDENTS_2.matcher(ex.getMessage()); if (matcher.find()) { return new DbNotUniqueException(id, makeIdent(matcher.group(1)), makeIdent(matcher.group(2)), ex); } break; case 1400: // insert null matcher = EXCEPTION_IDENTS_3.matcher(ex.getMessage()); if (matcher.find()) { return new DbNotNullableException(id, makeIdent(matcher.group(1)), makeIdent(matcher.group(2)), makeIdent(matcher.group(3)), ex); } break; case 2292: // delete referential entry matcher = EXCEPTION_IDENTS_2.matcher(ex.getMessage()); if (matcher.find()) { return new DbReferentialIntegrityException(id, makeIdent(matcher.group(1)), makeIdent(matcher.group(2)), ex); } break; } } } catch (Exception ex2) { // log this exception... LOG.warn("Exception thrown during wrapSQLException", ex2); // ...but throw the original SQLException } return super.wrapSQLException(id, message, ex); }