Search in sources :

Example 1 with Optional

use of com.wplatform.ddal.excutor.Optional in project jdbc-shards by wplatform.

the class JdbcCallWorker method doWork.

@Override
public Integer doWork() {
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        DataSource dataSource = getDataSource();
        Optional optional = Optional.build().shardName(shardName).readOnly(false);
        if (trace.isDebugEnabled()) {
            trace.debug("{0} Fetching connection from DataSource.", shardName);
        }
        conn = session.applyConnection(dataSource, optional);
        attach(conn);
        if (trace.isDebugEnabled()) {
            trace.debug("{0} Preparing: {1};", shardName, sql);
        }
        stmt = conn.prepareStatement(sql);
        attach(stmt);
        applyQueryTimeout(stmt);
        if (params != null) {
            for (int i = 0, size = params.size(); i < size; i++) {
                Value v = params.get(i);
                v.set(stmt, i + 1);
                if (trace.isDebugEnabled()) {
                    trace.debug("{0} setParameter: {1} -> {2};", shardName, i + 1, v.getSQL());
                }
            }
        }
        int rows = stmt.executeUpdate();
        if (trace.isDebugEnabled()) {
            trace.debug("{0} executeUpdate: {1} affected.", shardName, rows);
        }
        return rows;
    } catch (SQLException e) {
        StatementBuilder buff = new StatementBuilder();
        buff.append(shardName).append(" executing executeUpdate error:").append(sql);
        if (params != null && params.size() > 0) {
            buff.append("\n{");
            int i = 1;
            for (Value v : params) {
                buff.appendExceptFirst(", ");
                buff.append(i++).append(": ").append(v.getSQL());
            }
            buff.append('}');
        }
        buff.append(';');
        trace.error(e, buff.toString());
        throw wrapException(sql, e);
    }
}
Also used : Optional(com.wplatform.ddal.excutor.Optional) SQLException(java.sql.SQLException) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Connection(java.sql.Connection) Value(com.wplatform.ddal.value.Value) PreparedStatement(java.sql.PreparedStatement) DataSource(javax.sql.DataSource)

Example 2 with Optional

use of com.wplatform.ddal.excutor.Optional in project jdbc-shards by wplatform.

the class TableMate method readMataData.

/**
     * @param session
     */
public void readMataData(Session session, TableNode matadataNode) {
    for (int retry = 0; ; retry++) {
        try {
            Connection conn = null;
            String tableName = matadataNode.getCompositeObjectName();
            String shardName = matadataNode.getShardName();
            try {
                DataSourceRepository dsRepository = session.getDataSourceRepository();
                DataSource dataSource = dsRepository.getDataSourceByShardName(shardName);
                Optional optional = Optional.build().shardName(shardName).readOnly(false);
                conn = session.applyConnection(dataSource, optional);
                tableName = database.identifier(tableName);
                tryReadMetaData(conn, tableName);
                return;
            } catch (Exception e) {
                throw DbException.convert(e);
            } finally {
                JdbcUtils.closeSilently(conn);
            }
        } catch (DbException e) {
            if (retry >= MAX_RETRY) {
                throw e;
            }
        }
    }
}
Also used : DataSourceRepository(com.wplatform.ddal.shards.DataSourceRepository) Optional(com.wplatform.ddal.excutor.Optional) Connection(java.sql.Connection) SQLException(java.sql.SQLException) DbException(com.wplatform.ddal.message.DbException) DataSource(javax.sql.DataSource) DbException(com.wplatform.ddal.message.DbException)

Aggregations

Optional (com.wplatform.ddal.excutor.Optional)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 DataSource (javax.sql.DataSource)2 DbException (com.wplatform.ddal.message.DbException)1 DataSourceRepository (com.wplatform.ddal.shards.DataSourceRepository)1 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1 Value (com.wplatform.ddal.value.Value)1 PreparedStatement (java.sql.PreparedStatement)1