1 16 17 package org.springframework.jdbc.support.incrementer; 18 19 import java.sql.Connection ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.sql.Statement ; 23 24 import org.springframework.dao.DataAccessException; 25 import org.springframework.dao.DataAccessResourceFailureException; 26 import org.springframework.jdbc.datasource.DataSourceUtils; 27 import org.springframework.jdbc.support.JdbcUtils; 28 29 37 public abstract class AbstractSequenceMaxValueIncrementer extends AbstractDataFieldMaxValueIncrementer { 38 39 protected long getNextKey() throws DataAccessException { 40 Connection con = DataSourceUtils.getConnection(getDataSource()); 41 Statement stmt = null; 42 ResultSet rs = null; 43 try { 44 stmt = con.createStatement(); 45 DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); 46 rs = stmt.executeQuery(getSequenceQuery()); 47 if (rs.next()) { 48 return rs.getLong(1); 49 } 50 else { 51 throw new DataAccessResourceFailureException("Sequence query did not return a result"); 52 } 53 } 54 catch (SQLException ex) { 55 throw new DataAccessResourceFailureException("Could not obtain sequence value", ex); 56 } 57 finally { 58 JdbcUtils.closeResultSet(rs); 59 JdbcUtils.closeStatement(stmt); 60 DataSourceUtils.releaseConnection(con, getDataSource()); 61 } 62 } 63 64 67 protected abstract String getSequenceQuery(); 68 69 } 70 | Popular Tags |