}
} finally {
ec.unlock();
}
} else {
ERXJDBCConnectionBroker broker = ERXJDBCConnectionBroker.connectionBrokerForEntityNamed(entityName);
Connection con = broker.getConnection();
try {
try {
con.setAutoCommit(false);
con.setReadOnly(false);
} catch (SQLException e) {
log.error(e, e);
}
for(int tries = 0; tries < count; tries++) {
try {
ResultSet resultSet = con.createStatement().executeQuery("select pk_value from pk_table " + where);
con.commit();
boolean hasNext = resultSet.next();
long pk = 1;
if (hasNext) {
pk = resultSet.getLong("pk_value");
// now execute the update
con.createStatement().executeUpdate("update pk_table set pk_value = " + (pk+increasePkBy) + " " + where);
} else {
pk = maxIdFromTable(entityName);
// first time, we need to set i up
con.createStatement().executeUpdate("insert into pk_table (eoentity_name, pk_value) values ('" + entityName + "', " + (pk+increasePkBy) + ")");
}
con.commit();
return Long.valueOf(pk);
} catch(SQLException ex) {
String s = ex.getMessage().toLowerCase();
boolean creationError = (s.indexOf("error code 116") != -1); // frontbase?
creationError |= (s.indexOf("pk_table") != -1 && s.indexOf("does not exist") != -1); // postgres ?
creationError |= s.indexOf("ora-00942") != -1; // oracle
if (creationError) {
try {
con.rollback();
log.info("creating pk table");
con.createStatement().executeUpdate("create table pk_table (eoentity_name varchar(100) not null, pk_value integer)");
con.createStatement().executeUpdate("alter table pk_table add primary key (eoentity_name)");// NOT
// DEFERRABLE
// INITIALLY
// IMMEDIATE");
con.commit();
} catch (SQLException ee) {
throw new NSForwardException(ee, "could not create pk table");
}
} else {
throw new NSForwardException(ex, "Error fetching PK");
}
}
}
} finally {
broker.freeConnection(con);
}
}
throw new IllegalStateException("Couldn't get PK");
}