DefaultSequenceDao extends AbstractLifecycle implements SequenceDao { public SequenceRange nextRange(String name) throws SequenceException { if (name == null) { throw new IllegalArgumentException("序列名称不能为空"); } long oldValue; long newValue; Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; for (int i = 0; i < retryTimes + 1; ++i) { try { conn = dataSource.getConnection(); stmt = conn.prepareStatement(getSelectSql()); stmt.setString(1, name); rs = stmt.executeQuery(); rs.next(); oldValue = rs.getLong(1); if (oldValue < 0) { StringBuilder message = new StringBuilder(); message.append("Sequence value cannot be less than zero, value = ").append(oldValue); message.append(", please check table ").append(getTableName()); throw new SequenceException(message.toString()); } if (oldValue > Long.MAX_VALUE - DELTA) { StringBuilder message = new StringBuilder(); message.append("Sequence value overflow, value = ").append(oldValue); message.append(", please check table ").append(getTableName()); throw new SequenceException(message.toString()); } newValue = oldValue + getStep(); } catch (SQLException e) { throw new SequenceException(e); } finally { closeResultSet(rs); rs = null; closeStatement(stmt); stmt = null; closeConnection(conn); conn = null; } try { conn = dataSource.getConnection(); stmt = conn.prepareStatement(getUpdateSql()); stmt.setLong(1, newValue); stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis())); stmt.setString(3, name); stmt.setLong(4, oldValue); int affectedRows = stmt.executeUpdate(); if (affectedRows == 0) { continue; } return new SequenceRange(oldValue + 1, newValue); } catch (SQLException e) { throw new SequenceException(e); } finally { closeStatement(stmt); stmt = null; closeConnection(conn); conn = null; } } throw new SequenceException("Retried too many times, retryTimes = " + retryTimes); }  SequenceRange nextRange(String name); DataSource getDataSource(); void setDataSource(DataSource dataSource); int getRetryTimes(); void setRetryTimes(int retryTimes); int getStep(); void setStep(int step); String getTableName(); void setTableName(String tableName); String getNameColumnName(); void setNameColumnName(String nameColumnName); String getValueColumnName(); void setValueColumnName(String valueColumnName); String getGmtModifiedColumnName(); void setGmtModifiedColumnName(String gmtModifiedColumnName);  }
@Ignore @Test public void test_nextRange() { try { SequenceRange sequenceRange = sequenceDao.nextRange("xdh"); System.out.println(sequenceRange); } catch (SequenceException e) { e.printStackTrace(); } }