protected synchronized int getPrimaryKeyId() {
		if (primaryKeyId == -1) {
			ResultSet rs = null;
			Statement stat = null;
			try {
				stat = db.getConnection().createStatement();
				rs = stat.executeQuery("SELECT MAX(" + getTableIdCol() + ") FROM " + getTableName());
				primaryKeyId = rs.next() ? rs.getInt(1) : 0;
			} catch (SQLException e) {
				primaryKeyId = -1;
				log.log(Level.SEVERE, "getPrimaryKeyId failed: " + e.getMessage(), e);
			} finally {
				try {
					if (rs != null) rs.close();
					if (stat != null) stat.close();
				} catch (SQLException es) {
					log.log(Level.WARNING, "failed to close db resources: " + es.getMessage(), es);
				}
			}
		}
		primaryKeyId++;
		return primaryKeyId;
	}