|
|
|
@ -67,12 +67,7 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer |
|
|
|
/** The max id to serve */ |
|
|
|
/** The max id to serve */ |
|
|
|
private long maxId = 0; |
|
|
|
private long maxId = 0; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** Whether or not to use a new connection for the incrementer */ |
|
|
|
* Whether or not to use a new connection for the incrementer. Defaults to true |
|
|
|
|
|
|
|
* in order to support transactional storage engines. Set this to false if the storage engine |
|
|
|
|
|
|
|
* for the incrementer table is non-transactional like MYISAM and you prefer to not acquire |
|
|
|
|
|
|
|
* an additional database connection |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private boolean useNewConnection = true; |
|
|
|
private boolean useNewConnection = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -88,41 +83,30 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Convenience constructor. |
|
|
|
* Convenience constructor. |
|
|
|
* @param dataSource the DataSource to use |
|
|
|
* @param dataSource the DataSource to use |
|
|
|
* @param incrementerName the name of the sequence/table to use |
|
|
|
* @param incrementerName the name of the sequence table to use |
|
|
|
* @param columnName the name of the column in the sequence table to use |
|
|
|
* @param columnName the name of the column in the sequence table to use |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MySQLMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) { |
|
|
|
public MySQLMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) { |
|
|
|
super(dataSource, incrementerName, columnName); |
|
|
|
super(dataSource, incrementerName, columnName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Convenience constructor for setting whether to use a new connection for the incrementer. |
|
|
|
|
|
|
|
* @param dataSource the DataSource to use |
|
|
|
|
|
|
|
* @param incrementerName the name of the sequence/table to use |
|
|
|
|
|
|
|
* @param columnName the name of the column in the sequence table to use |
|
|
|
|
|
|
|
* @param useNewConnection whether to use a new connection for the incrementer |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public MySQLMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName, |
|
|
|
|
|
|
|
boolean useNewConnection) { |
|
|
|
|
|
|
|
super(dataSource, incrementerName, columnName); |
|
|
|
|
|
|
|
this.useNewConnection = useNewConnection; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Return whether to use a new connection for the incrementer. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public boolean isUseNewConnection() { |
|
|
|
|
|
|
|
return useNewConnection; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set whether to use a new connection for the incrementer. |
|
|
|
* Set whether to use a new connection for the incrementer. |
|
|
|
|
|
|
|
* <p>{@code true} is necessary to support transactional storage engines, |
|
|
|
|
|
|
|
* using an isolated separate transaction for the increment operation. |
|
|
|
|
|
|
|
* {@code false} is sufficient if the storage engine of the sequence table |
|
|
|
|
|
|
|
* is non-transactional (like MYISAM), avoiding the effort of acquiring an |
|
|
|
|
|
|
|
* extra {@code Connection} for the increment operation. |
|
|
|
|
|
|
|
* <p>Default is {@code true} since Spring Framework 5.0. |
|
|
|
|
|
|
|
* @since 4.3.6 |
|
|
|
|
|
|
|
* @see DataSource#getConnection() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setUseNewConnection(boolean useNewConnection) { |
|
|
|
public void setUseNewConnection(boolean useNewConnection) { |
|
|
|
this.useNewConnection = useNewConnection; |
|
|
|
this.useNewConnection = useNewConnection; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected synchronized long getNextKey() throws DataAccessException { |
|
|
|
protected synchronized long getNextKey() throws DataAccessException { |
|
|
|
if (this.maxId == this.nextId) { |
|
|
|
if (this.maxId == this.nextId) { |
|
|
|
@ -138,7 +122,7 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer |
|
|
|
Statement stmt = null; |
|
|
|
Statement stmt = null; |
|
|
|
boolean mustRestoreAutoCommit = false; |
|
|
|
boolean mustRestoreAutoCommit = false; |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (useNewConnection) { |
|
|
|
if (this.useNewConnection) { |
|
|
|
con = getDataSource().getConnection(); |
|
|
|
con = getDataSource().getConnection(); |
|
|
|
if (con.getAutoCommit()) { |
|
|
|
if (con.getAutoCommit()) { |
|
|
|
mustRestoreAutoCommit = true; |
|
|
|
mustRestoreAutoCommit = true; |
|
|
|
@ -149,7 +133,7 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer |
|
|
|
con = DataSourceUtils.getConnection(getDataSource()); |
|
|
|
con = DataSourceUtils.getConnection(getDataSource()); |
|
|
|
} |
|
|
|
} |
|
|
|
stmt = con.createStatement(); |
|
|
|
stmt = con.createStatement(); |
|
|
|
if (!useNewConnection) { |
|
|
|
if (!this.useNewConnection) { |
|
|
|
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); |
|
|
|
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource()); |
|
|
|
} |
|
|
|
} |
|
|
|
// Increment the sequence column...
|
|
|
|
// Increment the sequence column...
|
|
|
|
@ -180,23 +164,23 @@ public class MySQLMaxValueIncrementer extends AbstractColumnMaxValueIncrementer |
|
|
|
} |
|
|
|
} |
|
|
|
finally { |
|
|
|
finally { |
|
|
|
JdbcUtils.closeStatement(stmt); |
|
|
|
JdbcUtils.closeStatement(stmt); |
|
|
|
if (useNewConnection) { |
|
|
|
if (con != null) { |
|
|
|
try { |
|
|
|
if (this.useNewConnection) { |
|
|
|
con.commit(); |
|
|
|
try { |
|
|
|
if (mustRestoreAutoCommit) { |
|
|
|
con.commit(); |
|
|
|
con.setAutoCommit(true); |
|
|
|
if (mustRestoreAutoCommit) { |
|
|
|
|
|
|
|
con.setAutoCommit(true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (SQLException ignore) { |
|
|
|
|
|
|
|
throw new DataAccessResourceFailureException( |
|
|
|
|
|
|
|
"Unable to commit new sequence value changes for " + getIncrementerName()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
JdbcUtils.closeConnection(con); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (SQLException ignore) { |
|
|
|
else { |
|
|
|
throw new DataAccessResourceFailureException( |
|
|
|
DataSourceUtils.releaseConnection(con, getDataSource()); |
|
|
|
"Unable to commit new sequence value changes for " + getIncrementerName()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
|
|
|
|
con.close(); |
|
|
|
|
|
|
|
} catch (SQLException ignore) {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
DataSourceUtils.releaseConnection(con, getDataSource()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|