40 lines
1.4 KiB
Java
40 lines
1.4 KiB
Java
package com.interplug.qcast.config.batch;
|
|
|
|
import org.springframework.jdbc.support.incrementer.AbstractColumnMaxValueIncrementer;
|
|
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
|
|
|
import javax.sql.DataSource;
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
public class CustomIncrementer extends AbstractColumnMaxValueIncrementer {
|
|
|
|
public CustomIncrementer(DataSource dataSource, String incrementerName) {
|
|
super(dataSource, incrementerName, "ID");
|
|
}
|
|
|
|
@Override
|
|
protected long getNextKey() {
|
|
long nextKey = 0;
|
|
try (Connection conn = this.getDataSource().getConnection()) {
|
|
PreparedStatement psSelect = conn.prepareStatement("SELECT ID FROM " + getIncrementerName() + " WITH (UPDLOCK, HOLDLOCK)");
|
|
ResultSet rs = psSelect.executeQuery();
|
|
if (rs.next()) {
|
|
nextKey = rs.getLong(1);
|
|
}
|
|
rs.close();
|
|
psSelect.close();
|
|
|
|
PreparedStatement psUpdate = conn.prepareStatement("UPDATE " + getIncrementerName() + " SET ID = ?");
|
|
psUpdate.setLong(1, nextKey + 1);
|
|
psUpdate.executeUpdate();
|
|
psUpdate.close();
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException("Could not get next key for " + getIncrementerName(), e);
|
|
}
|
|
return nextKey + 1;
|
|
}
|
|
|
|
}
|