다음은 sequence incrementer를 지원하지 않는 Phoenix 을 사용하는 경우의 예시입니다.
<beans ...> <bean id="dao" class="com.poscoict.glueframework.dao.jdbc.GlueJdbcDao"> <property name="dataSource"><ref local="dataSource"/></property> <property name="queryManager"><ref local="queryManager"/></property> <property name="sequence"><ref local="sequence"/></property> </bean> <bean id="sequence" class="PhoenixSequence"> <constructor-arg><ref local="dataSource"/></constructor-arg> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.apache.phoenix.jdbc.PhoenixDriver"/> <property name="url" value="jdbc:phoenix:0.0.0.0:2181"/> </bean> </beans>
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.jdbc.datasource.DataSourceUtils; import org.springframework.jdbc.support.JdbcUtils; public class PhoenixSequence implements GlueSequence { private DataSource dataSource; public GluePhoenixSequence( DataSource ds ) { this.dataSource = ds; } public DataSource getDataSource() { return this.dataSource; } @Override public long getNextLongValue( String sequenceName ) { return getNextKey( sequenceName ); } @Override public int getNextIntValue( String sequenceName ) { return (int) getNextKey( sequenceName ); } @Override public String getNextStringValue( String sequenceName ) { return Long.toString( getNextKey( sequenceName ) ); } private String getSequenceQuery( String sequenceName ) { return "select NEXT VALUE FOR " + sequenceName + " from SYSTEM.\"SEQUENCE\" limit 1"; } private long getNextKey( String sequenceName ) throws DataAccessException { Connection con = DataSourceUtils.getConnection( getDataSource() ); Statement stmt = null; ResultSet rs = null; try { stmt = con.createStatement(); DataSourceUtils.applyTransactionTimeout( stmt, getDataSource() ); rs = stmt.executeQuery( this.getSequenceQuery( sequenceName ) ); if ( rs.next() ) { return rs.getLong( 1 ); } else { throw new DataAccessResourceFailureException( "Sequence query did not return a result" ); } } catch ( SQLException ex ) { throw new DataAccessResourceFailureException( "Could not obtain sequence value", ex ); } finally { JdbcUtils.closeResultSet( rs ); JdbcUtils.closeStatement( stmt ); DataSourceUtils.releaseConnection( con, getDataSource() ); } } }