Package er.extensions.jdbc

Examples of er.extensions.jdbc.ERXJDBCConnectionBroker


        }
      } 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");
  }
View Full Code Here


    if (entity == null) throw new NullPointerException("could not find an entity named " + ename);
    String tableName = entity.externalName();
    String colName = entity.primaryKeyAttributes().lastObject().columnName();
    String sql = "select max(" + colName + ") from " + tableName;

    ERXJDBCConnectionBroker broker = ERXJDBCConnectionBroker.connectionBrokerForEntityNamed(ename);
    Connection con = broker.getConnection();
    ResultSet resultSet;
    try {
      resultSet = con.createStatement().executeQuery(sql);
      con.commit();

      boolean hasNext = resultSet.next();
      long v = 1l;
      if (hasNext) {
        v = resultSet.getLong(1);
        if (log.isDebugEnabled())
          log.debug("received max id from table " + tableName + ", setting value in PK_TABLE to " + v);
        if(encodeEntityInPkValue()) {
          v = v >> CODE_LENGTH;
        }
        if(encodeHostInPkValue()) {
          v = v >> HOST_CODE_LENGTH;
        }
      }
      return v + 1;

    } catch (SQLException e) {
      log.error("could not call database with sql " + sql, e);
      throw new IllegalStateException("could not get value from " + sql);
    } finally {
      broker.freeConnection(con);
    }
  }
View Full Code Here

TOP

Related Classes of er.extensions.jdbc.ERXJDBCConnectionBroker

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.