当前位置: 首页>>代码示例>>Java>>正文


Java DataSourceUtils.applyTransactionTimeout方法代码示例

本文整理汇总了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());
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:29,代码来源:AbstractSequenceMaxValueIncrementer.java

示例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++];
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:42,代码来源:SybaseMaxValueIncrementer.java

示例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++];
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:42,代码来源:SqlServerMaxValueIncrementer.java

示例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;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:44,代码来源:MySQLMaxValueIncrementer.java

示例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++];
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:42,代码来源:DerbyMaxValueIncrementer.java

示例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++];
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:42,代码来源:HsqlMaxValueIncrementer.java

示例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;
 }
 
开发者ID:lodsve,项目名称:lodsve-framework,代码行数:73,代码来源:MySQLIDGenerator.java


注:本文中的org.springframework.jdbc.datasource.DataSourceUtils.applyTransactionTimeout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。