| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Hibernate
Revision: 15151
Author: steve.ebersole@jboss.com
Date: 29 Aug 2008 14:42:57
Changes:HHH-3456 : enhanced.SequenceStyleGenerator extensibility
Files:| ... | ...@@ -56,8 +56,9 @@ | |
| 56 | 56 | private final String valueColumnName; |
| 57 | 57 | private final int initialValue; |
| 58 | 58 | private final int incrementSize; |
| 59 | private final String select; | |
| 60 | private final String update; | |
| 59 | private final String selectQuery; | |
| 60 | private final String updateQuery; | |
| 61 | ||
| 61 | 62 | private boolean applyIncrementSizeToSourceValues; |
| 62 | 63 | private int accessCounter; |
| 63 | 64 | |
| ... | ...@@ -67,31 +68,46 @@ | |
| 67 | 68 | this.incrementSize = incrementSize; |
| 68 | 69 | this.valueColumnName = valueColumnName; |
| 69 | 70 | |
| 70 | select = "select " + valueColumnName + " id_val" + | |
| 71 | selectQuery = "select " + valueColumnName + " id_val" + | |
| 71 | 72 | " from " + dialect.appendLockHint( LockMode.UPGRADE, tableName ) + |
| 72 | 73 | dialect.getForUpdateString(); |
| 73 | 74 | |
| 74 | update = "update " + tableName + | |
| 75 | updateQuery = "update " + tableName + | |
| 75 | 76 | " set " + valueColumnName + "= ?" + |
| 76 | 77 | " where " + valueColumnName + "=?"; |
| 77 | 78 | } |
| 78 | 79 | |
| 80 | /** | |
| 81 | * {@inheritDoc} | |
| 82 | */ | |
| 79 | 83 | public String getName() { |
| 80 | 84 | return tableName; |
| 81 | 85 | } |
| 82 | 86 | |
| 87 | /** | |
| 88 | * {@inheritDoc} | |
| 89 | */ | |
| 83 | 90 | public int getIncrementSize() { |
| 84 | 91 | return incrementSize; |
| 85 | 92 | } |
| 86 | 93 | |
| 94 | /** | |
| 95 | * {@inheritDoc} | |
| 96 | */ | |
| 87 | 97 | public int getTimesAccessed() { |
| 88 | 98 | return accessCounter; |
| 89 | 99 | } |
| 90 | 100 | |
| 101 | /** | |
| 102 | * {@inheritDoc} | |
| 103 | */ | |
| 91 | 104 | public void prepare(Optimizer optimizer) { |
| 92 | 105 | applyIncrementSizeToSourceValues = optimizer.applyIncrementSizeToSourceValues(); |
| 93 | 106 | } |
| 94 | 107 | |
| 108 | /** | |
| 109 | * {@inheritDoc} | |
| 110 | */ | |
| 95 | 111 | public AccessCallback buildCallback(final SessionImplementor session) { |
| 96 | 112 | return new AccessCallback() { |
| 97 | 113 | public long getNextValue() { |
| ... | ...@@ -100,6 +116,9 @@ | |
| 100 | 116 | }; |
| 101 | 117 | } |
| 102 | 118 | |
| 119 | /** | |
| 120 | * {@inheritDoc} | |
| 121 | */ | |
| 103 | 122 | public String[] sqlCreateStrings(Dialect dialect) throws HibernateException { |
| 104 | 123 | return new String[] { |
| 105 | 124 | dialect.getCreateTableString() + " " + tableName + " ( " + valueColumnName + " " + dialect.getTypeName( Types.BIGINT ) + " )", |
| ... | ...@@ -107,6 +126,9 @@ | |
| 107 | 126 | }; |
| 108 | 127 | } |
| 109 | 128 | |
| 129 | /** | |
| 130 | * {@inheritDoc} | |
| 131 | */ | |
| 110 | 132 | public String[] sqlDropStrings(Dialect dialect) throws HibernateException { |
| 111 | 133 | StringBuffer sqlDropString = new StringBuffer().append( "drop table " ); |
| 112 | 134 | if ( dialect.supportsIfExistsBeforeTableName() ) { |
| ... | ...@@ -119,46 +141,47 @@ | |
| 119 | 141 | return new String[] { sqlDropString.toString() }; |
| 120 | 142 | } |
| 121 | 143 | |
| 144 | /** | |
| 145 | * {@inheritDoc} | |
| 146 | */ | |
| 122 | 147 | protected Serializable doWorkInCurrentTransaction(Connection conn, String sql) throws SQLException { |
| 123 | 148 | long result; |
| 124 | 149 | int rows; |
| 125 | 150 | do { |
| 126 | sql = select; | |
| 127 | SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC ); | |
| 128 | PreparedStatement qps = conn.prepareStatement( select ); | |
| 151 | SQL_STATEMENT_LOGGER.logStatement( selectQuery, FormatStyle.BASIC ); | |
| 152 | PreparedStatement selectPS = conn.prepareStatement( selectQuery ); | |
| 129 | 153 | try { |
| 130 | ResultSet rs = qps.executeQuery(); | |
| 131 | if ( !rs.next() ) { | |
| 154 | ResultSet selectRS = selectPS.executeQuery(); | |
| 155 | if ( !selectRS.next() ) { | |
| 132 | 156 | String err = "could not read a hi value - you need to populate the table: " + tableName; |
| 133 | 157 | log.error( err ); |
| 134 | 158 | throw new IdentifierGenerationException( err ); |
| 135 | 159 | } |
| 136 | result = rs.getLong( 1 ); | |
| 137 | rs.close(); | |
| 160 | result = selectRS.getLong( 1 ); | |
| 161 | selectRS.close(); | |
| 138 | 162 | } |
| 139 | 163 | catch ( SQLException sqle ) { |
| 140 | 164 | log.error( "could not read a hi value", sqle ); |
| 141 | 165 | throw sqle; |
| 142 | 166 | } |
| 143 | 167 | finally { |
| 144 | qps.close(); | |
| 168 | selectPS.close(); | |
| 145 | 169 | } |
| 146 | 170 | |
| 147 | sql = update; | |
| 148 | SQL_STATEMENT_LOGGER.logStatement( sql, FormatStyle.BASIC ); | |
| 149 | PreparedStatement ups = conn.prepareStatement( update ); | |
| 171 | SQL_STATEMENT_LOGGER.logStatement( updateQuery, FormatStyle.BASIC ); | |
| 172 | PreparedStatement updatePS = conn.prepareStatement( updateQuery ); | |
| 150 | 173 | try { |
| 151 | 174 | int increment = applyIncrementSizeToSourceValues ? incrementSize : 1; |
| 152 | ups.setLong( 1, result + increment ); | |
| 153 | ups.setLong( 2, result ); | |
| 154 | rows = ups.executeUpdate(); | |
| 175 | updatePS.setLong( 1, result + increment ); | |
| 176 | updatePS.setLong( 2, result ); | |
| 177 | rows = updatePS.executeUpdate(); | |
| 155 | 178 | } |
| 156 | 179 | catch ( SQLException sqle ) { |
| 157 | log.error( "could not update hi value in: " + tableName, sqle ); | |
| 180 | log.error( "could not updateQuery hi value in: " + tableName, sqle ); | |
| 158 | 181 | throw sqle; |
| 159 | 182 | } |
| 160 | 183 | finally { |
| 161 | ups.close(); | |
| 184 | updatePS.close(); | |
| 162 | 185 | } |
| 163 | 186 | } while ( rows == 0 ); |
| 164 | 187 |
| ... | ...@@ -58,18 +58,30 @@ | |
| 58 | 58 | sql = dialect.getSequenceNextValString( sequenceName ); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | /** | |
| 62 | * {@inheritDoc} | |
| 63 | */ | |
| 61 | 64 | public String getName() { |
| 62 | 65 | return sequenceName; |
| 63 | 66 | } |
| 64 | 67 | |
| 68 | /** | |
| 69 | * {@inheritDoc} | |
| 70 | */ | |
| 65 | 71 | public int getIncrementSize() { |
| 66 | 72 | return incrementSize; |
| 67 | 73 | } |
| 68 | 74 | |
| 75 | /** | |
| 76 | * {@inheritDoc} | |
| 77 | */ | |
| 69 | 78 | public int getTimesAccessed() { |
| 70 | 79 | return accessCounter; |
| 71 | 80 | } |
| 72 | 81 | |
| 82 | /** | |
| 83 | * {@inheritDoc} | |
| 84 | */ | |
| 73 | 85 | public AccessCallback buildCallback(final SessionImplementor session) { |
| 74 | 86 | return new AccessCallback() { |
| 75 | 87 | public long getNextValue() { |
| ... | ...@@ -112,15 +124,24 @@ | |
| 112 | 124 | }; |
| 113 | 125 | } |
| 114 | 126 | |
| 127 | /** | |
| 128 | * {@inheritDoc} | |
| 129 | */ | |
| 115 | 130 | public void prepare(Optimizer optimizer) { |
| 116 | 131 | applyIncrementSizeToSourceValues = optimizer.applyIncrementSizeToSourceValues(); |
| 117 | 132 | } |
| 118 | 133 | |
| 134 | /** | |
| 135 | * {@inheritDoc} | |
| 136 | */ | |
| 119 | 137 | public String[] sqlCreateStrings(Dialect dialect) throws HibernateException { |
| 120 | 138 | int sourceIncrementSize = applyIncrementSizeToSourceValues ? incrementSize : 1; |
| 121 | 139 | return dialect.getCreateSequenceStrings( sequenceName, initialValue, sourceIncrementSize ); |
| 122 | 140 | } |
| 123 | 141 | |
| 142 | /** | |
| 143 | * {@inheritDoc} | |
| 144 | */ | |
| 124 | 145 | public String[] sqlDropStrings(Dialect dialect) throws HibernateException { |
| 125 | 146 | return dialect.getDropSequenceStrings( sequenceName ); |
| 126 | 147 | } |
| ... | ...@@ -76,10 +76,19 @@ | |
| 76 | 76 | return new NoopOptimizer( returnClass, incrementSize ); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | /** | |
| 80 | * Common support for optimizer implementations. | |
| 81 | */ | |
| 79 | 82 | public static abstract class OptimizerSupport implements Optimizer { |
| 80 | 83 | protected final Class returnClass; |
| 81 | 84 | protected final int incrementSize; |
| 82 | 85 | |
| 86 | /** | |
| 87 | * Construct an optimizer | |
| 88 | * | |
| 89 | * @param returnClass The expected id class. | |
| 90 | * @param incrementSize The increment size | |
| 91 | */ | |
| 83 | 92 | protected OptimizerSupport(Class returnClass, int incrementSize) { |
| 84 | 93 | if ( returnClass == null ) { |
| 85 | 94 | throw new HibernateException( "return class is required" ); |
| ... | ...@@ -88,19 +97,39 @@ | |
| 88 | 97 | this.incrementSize = incrementSize; |
| 89 | 98 | } |
| 90 | 99 | |
| 91 | protected Serializable make(long value) { | |
| 100 | /** | |
| 101 | * Take the primitive long value and "make" (or wrap) it into the | |
| 102 | * {@link #getReturnClass id type}. | |
| 103 | * | |
| 104 | * @param value The primitive value to make/wrap. | |
| 105 | * @return The wrapped value. | |
| 106 | */ | |
| 107 | protected final Serializable make(long value) { | |
| 92 | 108 | return IdentifierGeneratorFactory.createNumber( value, returnClass ); |
| 93 | 109 | } |
| 94 | 110 | |
| 95 | public Class getReturnClass() { | |
| 111 | /** | |
| 112 | * Getter for property 'returnClass'. This is the Java | |
| 113 | * class which is used to represent the id (e.g. {@link java.lang.Long}). | |
| 114 | * | |
| 115 | * @return Value for property 'returnClass'. | |
| 116 | */ | |
| 117 | public final Class getReturnClass() { | |
| 96 | 118 | return returnClass; |
| 97 | 119 | } |
| 98 | 120 | |
| 99 | public int getIncrementSize() { | |
| 121 | /** | |
| 122 | * {@inheritDoc} | |
| 123 | */ | |
| 124 | public final int getIncrementSize() { | |
| 100 | 125 | return incrementSize; |
| 101 | 126 | } |
| 102 | 127 | } |
| 103 | 128 | |
| 129 | /** | |
| 130 | * An optimizer that performs no optimization. The database is hit for | |
| 131 | * every request. | |
| 132 | */ | |
| 104 | 133 | public static class NoopOptimizer extends OptimizerSupport { |
| 105 | 134 | private long lastSourceValue = -1; |
| 106 | 135 | |
| ... | ...@@ -108,6 +137,9 @@ | |
| 108 | 137 | super( returnClass, incrementSize ); |
| 109 | 138 | } |
| 110 | 139 | |
| 140 | /** | |
| 141 | * {@inheritDoc} | |
| 142 | */ | |
| 111 | 143 | public Serializable generate(AccessCallback callback) { |
| 112 | 144 | if ( lastSourceValue == -1 ) { |
| 113 | 145 | while( lastSourceValue <= 0 ) { |
| ... | ...@@ -120,15 +152,25 @@ | |
| 120 | 152 | return make( lastSourceValue ); |
| 121 | 153 | } |
| 122 | 154 | |
| 155 | /** | |
| 156 | * {@inheritDoc} | |
| 157 | */ | |
| 123 | 158 | public long getLastSourceValue() { |
| 124 | 159 | return lastSourceValue; |
| 125 | 160 | } |
| 126 | 161 | |
| 162 | /** | |
| 163 | * {@inheritDoc} | |
| 164 | */ | |
| 127 | 165 | public boolean applyIncrementSizeToSourceValues() { |
| 128 | 166 | return false; |
| 129 | 167 | } |
| 130 | 168 | } |
| 131 | 169 | |
| 170 | /** | |
| 171 | * Optimizer which applies a 'hilo' algorithm in memory to achieve | |
| 172 | * optimization. | |
| 173 | */ | |
| 132 | 174 | public static class HiLoOptimizer extends OptimizerSupport { |
| 133 | 175 | private long lastSourceValue = -1; |
| 134 | 176 | private long value; |
| ... | ...@@ -144,6 +186,9 @@ | |
| 144 | 186 | } |
| 145 | 187 | } |
| 146 | 188 | |
| 189 | /** | |
| 190 | * {@inheritDoc} | |
| 191 | */ | |
| 147 | 192 | public synchronized Serializable generate(AccessCallback callback) { |
| 148 | 193 | if ( lastSourceValue < 0 ) { |
| 149 | 194 | lastSourceValue = callback.getNextValue(); |
| ... | ...@@ -161,23 +206,43 @@ | |
| 161 | 206 | } |
| 162 | 207 | |
| 163 | 208 | |
| 209 | /** | |
| 210 | * {@inheritDoc} | |
| 211 | */ | |
| 164 | 212 | public long getLastSourceValue() { |
| 165 | 213 | return lastSourceValue; |
| 166 | 214 | } |
| 167 | 215 | |
| 216 | /** | |
| 217 | * {@inheritDoc} | |
| 218 | */ | |
| 168 | 219 | public boolean applyIncrementSizeToSourceValues() { |
| 169 | 220 | return false; |
| 170 | 221 | } |
| 171 | 222 | |
| 223 | /** | |
| 224 | * Getter for property 'lastValue'. | |
| 225 | * | |
| 226 | * @return Value for property 'lastValue'. | |
| 227 | */ | |
| 172 | 228 | public long getLastValue() { |
| 173 | 229 | return value - 1; |
| 174 | 230 | } |
| 175 | 231 | |
| 232 | /** | |
| 233 | * Getter for property 'hiValue'. | |
| 234 | * | |
| 235 | * @return Value for property 'hiValue'. | |
| 236 | */ | |
| 176 | 237 | public long getHiValue() { |
| 177 | 238 | return hiValue; |
| 178 | 239 | } |
| 179 | 240 | } |
| 180 | 241 | |
| 242 | /** | |
| 243 | * Optimizer which uses a pool of values, storing the next low value of the | |
| 244 | * range in the database. | |
| 245 | */ | |
| 181 | 246 | public static class PooledOptimizer extends OptimizerSupport { |
| 182 | 247 | private long value; |
| 183 | 248 | private long hiValue = -1; |
| ... | ...@@ -192,6 +257,9 @@ | |
| 192 | 257 | } |
| 193 | 258 | } |
| 194 | 259 | |
| 260 | /** | |
| 261 | * {@inheritDoc} | |
| 262 | */ | |
| 195 | 263 | public synchronized Serializable generate(AccessCallback callback) { |
| 196 | 264 | if ( hiValue < 0 ) { |
| 197 | 265 | value = callback.getNextValue(); |
| ... | ...@@ -211,14 +279,25 @@ | |
| 211 | 279 | return make( value++ ); |
| 212 | 280 | } |
| 213 | 281 | |
| 282 | /** | |
| 283 | * {@inheritDoc} | |
| 284 | */ | |
| 214 | 285 | public long getLastSourceValue() { |
| 215 | 286 | return hiValue; |
| 216 | 287 | } |
| 217 | 288 | |
| 289 | /** | |
| 290 | * {@inheritDoc} | |
| 291 | */ | |
| 218 | 292 | public boolean applyIncrementSizeToSourceValues() { |
| 219 | 293 | return true; |
| 220 | 294 | } |
| 221 | 295 | |
| 296 | /** | |
| 297 | * Getter for property 'lastValue'. | |
| 298 | * | |
| 299 | * @return Value for property 'lastValue'. | |
| 300 | */ | |
| 222 | 301 | public long getLastValue() { |
| 223 | 302 | return value - 1; |
| 224 | 303 | } |
| ... | ...@@ -123,14 +123,29 @@ | |
| 123 | 123 | private Optimizer optimizer; |
| 124 | 124 | private Type identifierType; |
| 125 | 125 | |
| 126 | /** | |
| 127 | * Getter for property 'databaseStructure'. | |
| 128 | * | |
| 129 | * @return Value for property 'databaseStructure'. | |
| 130 | */ | |
| 126 | 131 | public DatabaseStructure getDatabaseStructure() { |
| 127 | 132 | return databaseStructure; |
| 128 | 133 | } |
| 129 | 134 | |
| 135 | /** | |
| 136 | * Getter for property 'optimizer'. | |
| 137 | * | |
| 138 | * @return Value for property 'optimizer'. | |
| 139 | */ | |
| 130 | 140 | public Optimizer getOptimizer() { |
| 131 | 141 | return optimizer; |
| 132 | 142 | } |
| 133 | 143 | |
| 144 | /** | |
| 145 | * Getter for property 'identifierType'. | |
| 146 | * | |
| 147 | * @return Value for property 'identifierType'. | |
| 148 | */ | |
| 134 | 149 | public Type getIdentifierType() { |
| 135 | 150 | return identifierType; |
| 136 | 151 | } |
| ... | ...@@ -138,45 +153,160 @@ | |
| 138 | 153 | |
| 139 | 154 | // Configurable implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 140 | 155 | |
| 156 | /** | |
| 157 | * {@inheritDoc} | |
| 158 | */ | |
| 141 | 159 | public void configure(Type type, Properties params, Dialect dialect) throws MappingException { |
| 142 | identifierType = type; | |
| 160 | this.identifierType = type; | |
| 143 | 161 | boolean forceTableUse = PropertiesHelper.getBoolean( FORCE_TBL_PARAM, params, false ); |
| 144 | 162 | |
| 163 | final String sequenceName = determineSequenceName( params ); | |
| 164 | ||
| 165 | final int initialValue = determineInitialValue( params ); | |
| 166 | int incrementSize = determineIncrementSize( params ); | |
| 167 | ||
| 168 | final String optimizationStrategy = determineOptimizationStrategy( params, incrementSize ); | |
| 169 | incrementSize = determineAdjustedIncrementSize( optimizationStrategy, incrementSize ); | |
| 170 | ||
| 171 | if ( dialect.supportsSequences() && !forceTableUse ) { | |
| 172 | if ( OptimizerFactory.POOL.equals( optimizationStrategy ) && !dialect.supportsPooledSequences() ) { | |
| 173 | forceTableUse = true; | |
| 174 | log.info( | |
| 175 | "Forcing table use for sequence-style generator due to pooled optimizer selection where db does not support pooled sequences" | |
| 176 | ); | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 180 | this.databaseStructure = buildDatabaseStructure( params, dialect, forceTableUse, sequenceName, initialValue, incrementSize ); | |
| 181 | ||
| 182 | this.optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy, identifierType.getReturnedClass(), incrementSize ); | |
| 183 | this.databaseStructure.prepare( optimizer ); | |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Determine the name of the sequence (or table if this resolves to a physical table) | |
| 188 | * to use. | |
| 189 | * <p/> | |
| 190 | * Called during {@link #configure configuration}. | |
| 191 | * | |
| 192 | * @param params The params supplied in the generator config (plus some standard useful extras). | |
| 193 | * @return The sequence name | |
| 194 | */ | |
| 195 | protected String determineSequenceName(Properties params) { | |
| 145 | 196 | String sequenceName = PropertiesHelper.getString( SEQUENCE_PARAM, params, DEF_SEQUENCE_NAME ); |
| 146 | 197 | if ( sequenceName.indexOf( '.' ) < 0 ) { |
| 147 | 198 | String schemaName = params.getProperty( SCHEMA ); |
| 148 | 199 | String catalogName = params.getProperty( CATALOG ); |
| 149 | 200 | sequenceName = Table.qualify( catalogName, schemaName, sequenceName ); |
| 150 | 201 | } |
| 151 | int initialValue = PropertiesHelper.getInt( INITIAL_PARAM, params, DEFAULT_INITIAL_VALUE ); | |
| 152 | int incrementSize = PropertiesHelper.getInt( INCREMENT_PARAM, params, DEFAULT_INCREMENT_SIZE ); | |
| 153 | ||
| 154 | String valueColumnName = PropertiesHelper.getString( VALUE_COLUMN_PARAM, params, DEF_VALUE_COLUMN ); | |
| 202 | return sequenceName; | |
| 203 | } | |
| 155 | 204 | |
| 205 | /** | |
| 206 | * Determine the name of the column used to store the generator value in | |
| 207 | * the db. | |
| 208 | * <p/> | |
| 209 | * Called during {@link #configure configuration} <b>when resolving to a | |
| 210 | * physical table</b>. | |
| 211 | * | |
| 212 | * @param params The params supplied in the generator config (plus some standard useful extras). | |
| 213 | * @return The value column name | |
| 214 | */ | |
| 215 | protected String determineValueColumnName(Properties params) { | |
| 216 | return PropertiesHelper.getString( VALUE_COLUMN_PARAM, params, DEF_VALUE_COLUMN ); | |
| 217 | } | |
| 218 | ||
| 219 | /** | |
| 220 | * Determine the initial sequence value to use. This value is used when | |
| 221 | * initializing the {@link #getDatabaseStructure() database structure} | |
| 222 | * (i.e. sequence/table). | |
| 223 | * <p/> | |
| 224 | * Called during {@link #configure configuration}. | |
| 225 | * | |
| 226 | * @param params The params supplied in the generator config (plus some standard useful extras). | |
| 227 | * @return The initial value | |
| 228 | */ | |
| 229 | protected int determineInitialValue(Properties params) { | |
| 230 | return PropertiesHelper.getInt( INITIAL_PARAM, params, DEFAULT_INITIAL_VALUE ); | |
| 231 | } | |
| 232 | ||
| 233 | /** | |
| 234 | * Determine the increment size to be applied. The exact implications of | |
| 235 | * this value depends on the {@link #getOptimizer() optimizer} being used. | |
| 236 | * <p/> | |
| 237 | * Called during {@link #configure configuration}. | |
| 238 | * | |
| 239 | * @param params The params supplied in the generator config (plus some standard useful extras). | |
| 240 | * @return The increment size | |
| 241 | */ | |
| 242 | protected int determineIncrementSize(Properties params) { | |
| 243 | return PropertiesHelper.getInt( INCREMENT_PARAM, params, DEFAULT_INCREMENT_SIZE ); | |
| 244 | } | |
| 245 | ||
| 246 | /** | |
| 247 | * Determine the optimizer to use. | |
| 248 | * <p/> | |
| 249 | * Called during {@link #configure configuration}. | |
| 250 | * | |
| 251 | * @param params The params supplied in the generator config (plus some standard useful extras). | |
| 252 | * @param incrementSize The {@link #determineIncrementSize determined increment size} | |
| 253 | * @return The optimizer strategy (name) | |
| 254 | */ | |
| 255 | protected String determineOptimizationStrategy(Properties params, int incrementSize) { | |
| 156 | 256 | String defOptStrategy = incrementSize <= 1 ? OptimizerFactory.NONE : OptimizerFactory.POOL; |
| 157 | String optimizationStrategy = PropertiesHelper.getString( OPT_PARAM, params, defOptStrategy ); | |
| 257 | return PropertiesHelper.getString( OPT_PARAM, params, defOptStrategy ); | |
| 258 | } | |
| 259 | ||
| 260 | /** | |
| 261 | * In certain cases we need to adjust the increment size based on the | |
| 262 | * selected optimizer. This is the hook to achieve that. | |
| 263 | * | |
| 264 | * @param optimizationStrategy The optimizer strategy (name) | |
| 265 | * @param incrementSize The {@link #determineIncrementSize determined increment size} | |
| 266 | * @return The adjusted increment size. | |
| 267 | */ | |
| 268 | protected int determineAdjustedIncrementSize(String optimizationStrategy, int incrementSize) { | |
| 158 | 269 | if ( OptimizerFactory.NONE.equals( optimizationStrategy ) && incrementSize > 1 ) { |
| 159 | 270 | log.warn( "config specified explicit optimizer of [" + OptimizerFactory.NONE + "], but [" + INCREMENT_PARAM + "=" + incrementSize + "; honoring optimizer setting" ); |
| 160 | 271 | incrementSize = 1; |
| 161 | 272 | } |
| 162 | if ( dialect.supportsSequences() && !forceTableUse ) { | |
| 163 | if ( OptimizerFactory.POOL.equals( optimizationStrategy ) && !dialect.supportsPooledSequences() ) { | |
| 164 | // TODO : may even be better to fall back to a pooled table strategy here so that the db stored values remain consistent... | |
| 165 | optimizationStrategy = OptimizerFactory.HILO; | |
| 166 | } | |
| 167 | databaseStructure = new SequenceStructure( dialect, sequenceName, initialValue, incrementSize ); | |
| 273 | return incrementSize; | |
| 274 | } | |
| 275 | ||
| 276 | /** | |
| 277 | * Build the database structure. | |
| 278 | * | |
| 279 | * @param params The params supplied in the generator config (plus some standard useful extras). | |
| 280 | * @param dialect The dialect being used. | |
| 281 | * @param forceTableUse Should a table be used even if the dialect supports sequences? | |
| 282 | * @param sequenceName The name to use for the sequence or table. | |
| 283 | * @param initialValue The initial value. | |
| 284 | * @param incrementSize the increment size to use (after any adjustments). | |
| 285 | * @return The db structure representation | |
| 286 | */ | |
| 287 | protected DatabaseStructure buildDatabaseStructure( | |
| 288 | Properties params, | |
| 289 | Dialect dialect, | |
| 290 | boolean forceTableUse, | |
| 291 | String sequenceName, | |
| 292 | int initialValue, | |
| 293 | int incrementSize) { | |
| 294 | boolean useSequence = dialect.supportsSequences() && !forceTableUse; | |
| 295 | if ( useSequence ) { | |
| 296 | return new SequenceStructure( dialect, sequenceName, initialValue, incrementSize ); | |
| 168 | 297 | } |
| 169 | 298 | else { |
| 170 | databaseStructure = new TableStructure( dialect, sequenceName, valueColumnName, initialValue, incrementSize ); | |
| 299 | String valueColumnName = determineValueColumnName( params ); | |
| 300 | return new TableStructure( dialect, sequenceName, valueColumnName, initialValue, incrementSize ); | |
| 171 | 301 | } |
| 172 | ||
| 173 | optimizer = OptimizerFactory.buildOptimizer( optimizationStrategy, identifierType.getReturnedClass(), incrementSize ); | |
| 174 | databaseStructure.prepare( optimizer ); | |
| 175 | 302 | } |
| 176 | 303 | |
| 177 | 304 | |
| 178 | 305 | // IdentifierGenerator implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 179 | 306 | |
| 307 | /** | |
| 308 | * {@inheritDoc} | |
| 309 | */ | |
| 180 | 310 | public Serializable generate(SessionImplementor session, Object object) throws HibernateException { |
| 181 | 311 | return optimizer.generate( databaseStructure.buildCallback( session ) ); |
| 182 | 312 | } |
| ... | ...@@ -184,16 +314,24 @@ | |
| 184 | 314 | |
| 185 | 315 | // PersistentIdentifierGenerator implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 186 | 316 | |
| 317 | /** | |
| 318 | * {@inheritDoc} | |
| 319 | */ | |
| 187 | 320 | public Object generatorKey() { |
| 188 | 321 | return databaseStructure.getName(); |
| 189 | 322 | } |
| 190 | 323 | |
| 324 | /** | |
| 325 | * {@inheritDoc} | |
| 326 | */ | |
| 191 | 327 | public String[] sqlCreateStrings(Dialect dialect) throws HibernateException { |
| 192 | 328 | return databaseStructure.sqlCreateStrings( dialect ); |
| 193 | 329 | } |
| 194 | 330 | |
| 331 | /** | |
| 332 | * {@inheritDoc} | |
| 333 | */ | |
| 195 | 334 | public String[] sqlDropStrings(Dialect dialect) throws HibernateException { |
| 196 | 335 | return databaseStructure.sqlDropStrings( dialect ); |
| 197 | 336 | } |
| 198 | ||
| 199 | 337 | } |