spring的JDBC框架中自增键的问题与cacheSize关系

public int nextIntValue() throws DataAccessException {
		return (int) getNextKey();
}

nextIntValue将调用getNextKey获取自增int值

@Override

         protected synchronized long getNextKey() throws DataAccessException{

                   if(this.maxId == this.nextId) {

                            /*

                            *Need to use straight JDBC code because we need to make sure that the insert andselect

                            *are performed on the same connection (otherwise we can't be sure thatlast_insert_id()

                            *returned the correct value)

                            */

                            Connection con = DataSourceUtils.getConnection(getDataSource());

                            Statement stmt = null;

                            try{

                                     stmt= con.createStatement();

                                     DataSourceUtils.applyTransactionTimeout(stmt,getDataSource());

                                     //Increment the sequence column...

                                     String columnName = getColumnName();

                                     stmt.executeUpdate("update"+ getIncrementerName() + " set " + columnName +

                                                        "= last_insert_id(" + columnName + " + " + getCacheSize() + ")");

                                     //Retrieve the new max of the sequence column...

                                     ResultSetr s = stmt.executeQuery(VALUE_SQL);

                                     try{

                                               if(!rs.next()) {

                                                        thrownew DataAccessResourceFailureException("last_insert_id() failedafter executing an update");

                                               }

                                               this.maxId= rs.getLong(1);

                                     }

                                     finally{

                                               JdbcUtils.closeResultSet(rs);

                                     }

                                     this.nextId= this.maxId - getCacheSize() + 1;

                            }

                            catch(SQLException ex) {

                                     throw new DataAccessResourceFailureException("Could not obtainlast_insert_id()", ex);

                            }

                            finally{

                                     JdbcUtils.closeStatement(stmt);

                                     DataSourceUtils.releaseConnection(con,getDataSource());

                            }

                   }

                   else{

                            this.nextId++;

                   }

                   return this.nextId;

         }


 

注明:以下讨论都是同一MySQLMaxValueIncrementer对象的基础上,因为nextId和maxId都是私有成员。

创建MySQLMaxValueIncrementer对象后,

第一次使用该对象调用nextIntValue时,maxId和nextId都为0

(这两个是私有成员且没有提供setter或构造函数,除了反射编程人员无法修改这两个值),

进入if中,修改列的值

stmt.executeUpdate("update "+getIncrementerName() + " set " + columnName +

                                                        "= last_insert_id(" + columnName + " + " + getCacheSize() + ")");

将每行的指定列值增加cacheSize,后通过select last_insert_id()查询最后一个修改的值赋值给maxId,nextId=maxId-cacheSize+1;


1.      如果设置cacheSize为1的话,每次nextId与maxId都相等,

在更新了列值后nextIntValue函数返回值就是当前列中最后一个值。


2.      如果设置cacheSize为0的话,每次在创建对象后第一次调用时进入更新当前列的值后maxId将为最后一个值,

nextId = maxId – 0 + 1; 之后nextId!=maxId将进入else执行,不停++,

该MySQLMaxValueIncrementer对象中nextId将不会再与maxId相同。


3.      如果设置cacheSize为负数时,与为0类似,只不过初始的nextId将大于maxId+1,之后自身++。


4.      如果设置cacheSize>1的话,则cacheSize作为一个缓存大小,执行cacheSize次的nextId++后会使得nextId==maxId成立,

进入if再次更新当前列值,然后返回一个maxId。


你可能感兴趣的:(spring,框架,jdbc,String,insert,setter)