001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.support.incrementer; 018 019import javax.sql.DataSource; 020 021/** 022 * {@link DataFieldMaxValueIncrementer} that increments the maximum value of a given SQL Server table 023 * with the equivalent of an auto-increment column. Note: If you use this class, your table key 024 * column should <i>NOT</i> be defined as an IDENTITY column, as the sequence table does the job. 025 * 026 * <p>This class is intended to be used with Microsoft SQL Server. 027 * 028 * <p>The sequence is kept in a table. There should be one sequence table per 029 * table that needs an auto-generated key. 030 * 031 * <p>Example: 032 * 033 * <pre class="code">create table tab (id int not null primary key, text varchar(100)) 034 * create table tab_sequence (id bigint identity) 035 * insert into tab_sequence default values</pre> 036 * 037 * If "cacheSize" is set, the intermediate values are served without querying the 038 * database. If the server or your application is stopped or crashes or a transaction 039 * is rolled back, the unused values will never be served. The maximum hole size in 040 * numbering is consequently the value of cacheSize. 041 * 042 * <b>HINT:</b> Since Microsoft SQL Server supports the JDBC 3.0 {@code getGeneratedKeys} 043 * method, it is recommended to use IDENTITY columns directly in the tables and then using a 044 * {@link org.springframework.jdbc.core.simple.SimpleJdbcInsert} or utilizing 045 * a {@link org.springframework.jdbc.support.KeyHolder} when calling the with the 046 * {@code update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)} 047 * method of the {@link org.springframework.jdbc.core.JdbcTemplate}. 048 * 049 * <p>Thanks to Preben Nilsson for the suggestion! 050 * 051 * @author Thomas Risberg 052 * @author Juergen Hoeller 053 * @since 2.5.5 054 */ 055public class SqlServerMaxValueIncrementer extends AbstractIdentityColumnMaxValueIncrementer { 056 057 /** 058 * Default constructor for bean property style usage. 059 * @see #setDataSource 060 * @see #setIncrementerName 061 * @see #setColumnName 062 */ 063 public SqlServerMaxValueIncrementer() { 064 } 065 066 /** 067 * Convenience constructor. 068 * @param dataSource the DataSource to use 069 * @param incrementerName the name of the sequence/table to use 070 * @param columnName the name of the column in the sequence table to use 071 */ 072 public SqlServerMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) { 073 super(dataSource, incrementerName, columnName); 074 } 075 076 077 @Override 078 protected String getIncrementStatement() { 079 return "insert into " + getIncrementerName() + " default values"; 080 } 081 082 @Override 083 protected String getIdentityStatement() { 084 return "select @@identity"; 085 } 086 087}