本文整理匯總了Java中org.springframework.jdbc.datasource.DataSourceUtils.applyTransactionTimeout方法的典型用法代碼示例。如果您正苦於以下問題:Java DataSourceUtils.applyTransactionTimeout方法的具體用法?Java DataSourceUtils.applyTransactionTimeout怎麽用?Java DataSourceUtils.applyTransactionTimeout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.jdbc.datasource.DataSourceUtils
的用法示例。
在下文中一共展示了DataSourceUtils.applyTransactionTimeout方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
/**
* Executes the SQL as specified by {@link #getSequenceQuery()}.
*/
@Override
protected long getNextKey() throws DataAccessException {
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
ResultSet rs = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
rs = stmt.executeQuery(getSequenceQuery());
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());
}
}
示例2: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
@Override
protected synchronized long getNextKey() throws DataAccessException {
if (this.nextValueIndex < 0 || this.nextValueIndex >= getCacheSize()) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same connection (otherwise we can't be sure that @@identity
* returns the correct value)
*/
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
this.valueCache = new long[getCacheSize()];
this.nextValueIndex = 0;
for (int i = 0; i < getCacheSize(); i++) {
stmt.executeUpdate(getIncrementStatement());
ResultSet rs = stmt.executeQuery(getIdentityStatement());
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("Identity statement failed after inserting");
}
this.valueCache[i] = rs.getLong(1);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
stmt.executeUpdate(getDeleteStatement(this.valueCache));
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not increment identity", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
return this.valueCache[this.nextValueIndex++];
}
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:41,代碼來源:AbstractIdentityColumnMaxValueIncrementer.java
示例3: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
@Override
protected synchronized long getNextKey() throws DataAccessException {
if (this.nextValueIndex < 0 || this.nextValueIndex >= getCacheSize()) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same connection (otherwise we can't be sure that @@identity
* returnes the correct value)
*/
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
this.valueCache = new long[getCacheSize()];
this.nextValueIndex = 0;
for (int i = 0; i < getCacheSize(); i++) {
stmt.executeUpdate(getIncrementStatement());
ResultSet rs = stmt.executeQuery("select @@identity");
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("@@identity failed after executing an update");
}
this.valueCache[i] = rs.getLong(1);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
long maxValue = this.valueCache[(this.valueCache.length - 1)];
stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + maxValue);
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not increment identity", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
return this.valueCache[this.nextValueIndex++];
}
示例4: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
@Override
protected synchronized long getNextKey() throws DataAccessException {
if (this.nextValueIndex < 0 || this.nextValueIndex >= getCacheSize()) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same connection (otherwise we can't be sure that @@identity
* returnes the correct value)
*/
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
this.valueCache = new long[getCacheSize()];
this.nextValueIndex = 0;
for (int i = 0; i < getCacheSize(); i++) {
stmt.executeUpdate("insert into " + getIncrementerName() + " default values");
ResultSet rs = stmt.executeQuery("select @@identity");
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("@@identity failed after executing an update");
}
this.valueCache[i] = rs.getLong(1);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
long maxValue = this.valueCache[(this.valueCache.length - 1)];
stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + maxValue);
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not increment identity", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
return this.valueCache[this.nextValueIndex++];
}
示例5: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
@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 and select
* are performed on the same connection (otherwise we can't be sure that last_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...
ResultSet rs = stmt.executeQuery(VALUE_SQL);
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("last_insert_id() failed after 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 obtain last_insert_id()", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
else {
this.nextId++;
}
return this.nextId;
}
示例6: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
@Override
protected synchronized long getNextKey() throws DataAccessException {
if (this.nextValueIndex < 0 || this.nextValueIndex >= getCacheSize()) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same connection (otherwise we can't be sure that last_insert_id()
* returned the correct value)
*/
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
this.valueCache = new long[getCacheSize()];
this.nextValueIndex = 0;
for (int i = 0; i < getCacheSize(); i++) {
stmt.executeUpdate("insert into " + getIncrementerName() + " (" + getDummyName() + ") values(null)");
ResultSet rs = stmt.executeQuery("select IDENTITY_VAL_LOCAL() from " + getIncrementerName());
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("IDENTITY_VAL_LOCAL() failed after executing an update");
}
this.valueCache[i] = rs.getLong(1);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
long maxValue = this.valueCache[(this.valueCache.length - 1)];
stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + maxValue);
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not obtain IDENTITY value", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
return this.valueCache[this.nextValueIndex++];
}
示例7: getNextKey
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
@Override
protected synchronized long getNextKey() throws DataAccessException {
if (this.nextValueIndex < 0 || this.nextValueIndex >= getCacheSize()) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same Connection. Otherwise we can't be sure that last_insert_id()
* returned the correct value.
*/
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
this.valueCache = new long[getCacheSize()];
this.nextValueIndex = 0;
for (int i = 0; i < getCacheSize(); i++) {
stmt.executeUpdate("insert into " + getIncrementerName() + " values(null)");
ResultSet rs = stmt.executeQuery("select max(identity()) from " + getIncrementerName());
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("identity() failed after executing an update");
}
this.valueCache[i] = rs.getLong(1);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
long maxValue = this.valueCache[(this.valueCache.length - 1)];
stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + maxValue);
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not obtain identity()", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
return this.valueCache[this.nextValueIndex++];
}
示例8: nextId
import org.springframework.jdbc.datasource.DataSourceUtils; //導入方法依賴的package包/類
@Override
public synchronized Long nextId(String sequenceName) {
if (NEXT_ID_CACHE.get(sequenceName) != null) {
this.nextId = NEXT_ID_CACHE.get(sequenceName);
}
if (MAX_ID_CACHE.get(sequenceName) != null) {
this.maxId = MAX_ID_CACHE.get(sequenceName);
}
if (this.maxId == this.nextId) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same connection (otherwise we can't be sure that last_insert_id()
* returned the correct value)
*/
Connection con = DataSourceUtils.getConnection(dataSource);
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, dataSource);
String updateSql = "update " + sequenceName + " set " + sequenceName + " = last_insert_id(" + sequenceName + " + " + getCacheSize() + ")";
try {
// Increment the sequence column...
stmt.executeUpdate(updateSql);
} catch (SQLException e) {
//發生異常,即不存在這張表
//1.執行新建這張表的語句
StringBuffer createSql = new StringBuffer();
createSql.append("create table ").append(sequenceName).append(" ( ");
createSql.append(sequenceName).append(" bigint(12) default null ");
createSql.append(" ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ");
stmt.execute(createSql.toString());
//2.初始化這張表的數據(將name的值置為0)
String initDataSql = "insert into " + sequenceName + " values(0)";
stmt.executeUpdate(initDataSql);
//3.Increment the sequence column...
stmt.executeUpdate(updateSql);
}
// Retrieve the new max of the sequence column...
ResultSet rs = stmt.executeQuery(VALUE_SQL);
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("last_insert_id() failed after executing an update");
}
this.maxId = rs.getLong(1);
//更新緩存
MAX_ID_CACHE.put(sequenceName, this.maxId);
} finally {
JdbcUtils.closeResultSet(rs);
}
this.nextId = this.maxId - getCacheSize() + 1;
//更新緩存
NEXT_ID_CACHE.put(sequenceName, this.nextId);
} catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not obtain last_insert_id()", ex);
} finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, dataSource);
}
} else {
this.nextId++;
//更新緩存
NEXT_ID_CACHE.put(sequenceName, this.nextId);
}
long result = this.nextId;
//初始化nextId和maxId
this.nextId = 0;
this.maxId = 0;
return result;
}