@Test public void testNextKey() throws Exception { SqliteMaxValueIncrementer mvi = new SqliteMaxValueIncrementer(dataSource, "max_value", "id"); assertEquals(1, mvi.getNextKey()); assertEquals(2, mvi.getNextKey()); assertEquals(3, mvi.getNextKey()); assertEquals(1, template.queryForObject("select count(*) from max_value", Integer.class).intValue()); } }
@Override protected long getNextKey() { Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null; try { stmt = con.createStatement(); DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); stmt.executeUpdate("insert into " + getIncrementerName() + " values(null)"); ResultSet rs = stmt.executeQuery("select max(rowid) from " + getIncrementerName()); if (!rs.next()) { throw new DataAccessResourceFailureException("rowid query failed after executing an update"); } long nextKey = rs.getLong(1); stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + nextKey); return nextKey; } catch (SQLException ex) { throw new DataAccessResourceFailureException("Could not obtain rowid", ex); } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } } }
return new SqliteMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName);
return new SqliteMaxValueIncrementer(dataSource, incrementerName, incrementerColumnName);
@Override protected long getNextKey() { Connection con = DataSourceUtils.getConnection(getDataSource()); Statement stmt = null; try { stmt = con.createStatement(); DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); stmt.executeUpdate("insert into " + getIncrementerName() + " values(null)"); ResultSet rs = stmt.executeQuery("select max(rowid) from " + getIncrementerName()); if (!rs.next()) { throw new DataAccessResourceFailureException("rowid query failed after executing an update"); } long nextKey = rs.getLong(1); stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + nextKey); return nextKey; } catch (SQLException ex) { throw new DataAccessResourceFailureException("Could not obtain rowid", ex); } finally { JdbcUtils.closeStatement(stmt); DataSourceUtils.releaseConnection(con, getDataSource()); } } }