1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| public class Sequence {private final static Log log = LogFactory.getLog(Sequence.class); private int blockSize = 5; private long startValue = 0; private final static String GET_SQL = "select id from sequence_value where name = ?"; private final static String NEW_SQL = "insert into sequence_value (id,name) values (?,?)"; private final static String UPDATE_SQL = "update sequence_value set id = ? where name = ? and id = ?";
private Map<String,Step> stepMap = new HashMap<String, Step>();
private boolean getNextBlock(String sequenceName, Step step) {Long value = getPersistenceValue(sequenceName); if (value == null) {try {value = newPersistenceValue(sequenceName); } catch (Exception e) {log.error("newPersistenceValue error!"); value = getPersistenceValue(sequenceName); } } boolean b = saveValue(value,sequenceName) == 1; if (b) {step.setCurrentValue(value); step.setEndValue(value+blockSize); } return b; }
public synchronized long get(String sequenceName) {Step step = stepMap.get(sequenceName); if(step ==null) {step = new Step(startValue,startValue+blockSize); stepMap.put(sequenceName, step); } else {if (step.currentValue < step.endValue) {return step.incrementAndGet(); } } for (int i = 0; i < blockSize; i++) {if (getNextBlock(sequenceName,step)) {return step.incrementAndGet(); } } throw new RuntimeException("No more value."); }
private int saveValue(long value, String sequenceName) { Connection connection = null; PreparedStatement statement = null; try {connection = dataSource.getConnection(); statement = connection.prepareStatement(UPDATE_SQL); statement.setLong(1, value + blockSize); statement.setString(2, sequenceName); statement.setLong(3, value); return statement.executeUpdate();} catch (Exception e) {log.error("newPersistenceValue error!", e); throw new RuntimeException("newPersistenceValue error!", e); } finally {if (statement != null) {try {statement.close(); } catch (SQLException e) {log.error("close statement error!", e); } } if (connection != null) {try {connection.close(); } catch (SQLException e) {log.error("close connection error!", e); } } } }
private Long getPersistenceValue(String sequenceName) { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try {connection = dataSource.getConnection(); statement = connection.prepareStatement(GET_SQL); statement.setString(1, sequenceName); resultSet = statement.executeQuery(); if (resultSet.next()) {return resultSet.getLong("id");} } catch (Exception e) {log.error("getPersistenceValue error!", e); throw new RuntimeException("getPersistenceValue error!", e); } finally {if (resultSet != null) {try {resultSet.close(); } catch (SQLException e) {log.error("close resultset error!", e); } } if (statement != null) {try {statement.close(); } catch (SQLException e) {log.error("close statement error!", e); } } if (connection != null) {try {connection.close(); } catch (SQLException e) {log.error("close connection error!", e); } } } return null; }
private Long newPersistenceValue(String sequenceName) { Connection connection = null; PreparedStatement statement = null; try {connection = dataSource.getConnection(); statement = connection.prepareStatement(NEW_SQL); statement.setLong(1, startValue); statement.setString(2, sequenceName); statement.executeUpdate();} catch (Exception e) {log.error("newPersistenceValue error!", e); throw new RuntimeException("newPersistenceValue error!", e); } finally {if (statement != null) {try {statement.close(); } catch (SQLException e) {log.error("close statement error!", e); } } if (connection != null) {try {connection.close(); } catch (SQLException e) {log.error("close connection error!", e); } } } return startValue; }
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}
public void setBlockSize(int blockSize) {this.blockSize = blockSize;}
public void setStartValue(long startValue) {this.startValue = startValue;}
static class Step { private long currentValue; private long endValue;
Step(long currentValue, long endValue) { this.currentValue = currentValue; this.endValue = endValue; }
public void setCurrentValue(long currentValue) {this.currentValue = currentValue;}
public void setEndValue(long endValue) {this.endValue = endValue;}
public long incrementAndGet() {return ++currentValue;} } }
|