예제 #1
0
 /** @param session */
 public void readMataData(Session session, ObjectNode matadataNode) {
   for (int retry = 0; ; retry++) {
     try {
       Connection conn = null;
       String shardName = matadataNode.getShardName();
       String tableName = matadataNode.getQualifiedObjectName();
       String catalog = matadataNode.getCatalog();
       String schema = matadataNode.getSchema();
       try {
         JdbcRepository dsRepository = (JdbcRepository) database.getRepository();
         DataSource dataSource = dsRepository.getDataSourceByShardName(shardName);
         conn = dataSource.getConnection();
         tableName = database.identifier(tableName);
         if (catalog != null) {
           catalog = database.identifier(catalog);
         }
         if (schema != null) {
           schema = database.identifier(schema);
         }
         tryReadMetaData(conn, catalog, schema, tableName);
         return;
       } catch (Exception e) {
         throw DbException.convert(e);
       } finally {
         JdbcUtils.closeSilently(conn);
       }
     } catch (DbException e) {
       if (retry >= MAX_RETRY) {
         throw e;
       }
     }
   }
 }
예제 #2
0
 /**
  * Encode the string as an URL.
  *
  * @param s the string to encode
  * @return the encoded string
  */
 public static String urlEncode(String s) {
   try {
     return URLEncoder.encode(s, "UTF-8");
   } catch (Exception e) {
     // UnsupportedEncodingException
     throw DbException.convert(e);
   }
 }
예제 #3
0
 public synchronized <T> List<T> foreach(Callback<T> callback) throws DbException {
   List<T> results = New.arrayList();
   for (String name : connectionMap.keySet()) {
     try {
       Connection conn = connectionMap.get(name);
       results.add(callback.handle(name, conn));
     } catch (SQLException e) {
       trace.error(e, "foreach {0} connection error", name);
       throw DbException.convert(e);
     }
   }
   return results;
 }
예제 #4
0
 private Connection getRawConnectionForReadOnly(Options options) {
   try {
     Connection conn = target.getConnection(options);
     conn.setAutoCommit(true);
     conn.setReadOnly(true);
     if (session.getTransactionIsolation() != 0) {
       if (conn.getTransactionIsolation() != session.getTransactionIsolation()) {
         conn.setTransactionIsolation(session.getTransactionIsolation());
       }
     }
     return conn;
   } catch (SQLException e) {
     throw DbException.convert(e);
   }
 }
예제 #5
0
 /**
  * Construct a local result set by reading all data from a regular result set.
  *
  * @param session the session
  * @param rs the result set
  * @param maxrows the maximum number of rows to read (0 for no limit)
  * @return the local result set
  */
 public static LocalResult read(Session session, ResultSet rs, int maxrows) {
   Expression[] cols = Expression.getExpressionColumns(session, rs);
   int columnCount = cols.length;
   LocalResult result = new LocalResult(session, cols, columnCount);
   try {
     for (int i = 0; (maxrows == 0 || i < maxrows) && rs.next(); i++) {
       Value[] list = new Value[columnCount];
       for (int j = 0; j < columnCount; j++) {
         int type = result.getColumnType(j);
         list[j] = DataType.readValue(rs, j + 1, type);
       }
       result.addRow(list);
     }
   } catch (SQLException e) {
     throw DbException.convert(e);
   }
   result.done();
   return result;
 }
예제 #6
0
 /**
  * @param options
  * @return
  * @throws SQLException
  */
 private Connection getRawConnection(Options options) throws DbException {
   Connection conn = target.getConnection(options);
   try {
     if (conn.getAutoCommit() != session.getAutoCommit()) {
       conn.setAutoCommit(session.getAutoCommit());
     }
     if (session.getTransactionIsolation() != 0) {
       if (conn.getTransactionIsolation() != session.getTransactionIsolation()) {
         conn.setTransactionIsolation(session.getTransactionIsolation());
       }
     }
     if (conn.isReadOnly() != session.isReadOnly()) {
       conn.setReadOnly(session.isReadOnly());
     }
   } catch (Exception e) {
     throw DbException.convert(e);
   }
   return conn;
 }