Search in sources :

Example 1 with StatementCallback

use of org.springframework.jdbc.core.StatementCallback in project cobar by alibaba.

the class CobarAdapter method getCurrentTimeMillis.

@Override
public Pair<Long, Long> getCurrentTimeMillis() {
    return (Pair<Long, Long>) getJdbcTemplate().execute(new StatementCallback() {

        @Override
        public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
            ResultSet rs = null;
            try {
                long time1 = System.currentTimeMillis();
                rs = stmt.executeQuery("show @@status.time");
                long time2 = System.currentTimeMillis();
                if (rs.next()) {
                    return new Pair<Long, Long>(time1 + (time2 - time1) / 2, rs.getLong(1));
                } else {
                    throw new IncorrectResultSizeDataAccessException(1, 0);
                }
            } finally {
                if (rs != null) {
                    rs.close();
                }
            }
        }
    });
}
Also used : StatementCallback(org.springframework.jdbc.core.StatementCallback) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Pair(com.alibaba.cobar.manager.util.Pair)

Aggregations

Pair (com.alibaba.cobar.manager.util.Pair)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 IncorrectResultSizeDataAccessException (org.springframework.dao.IncorrectResultSizeDataAccessException)1 StatementCallback (org.springframework.jdbc.core.StatementCallback)1