Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jdbctemplate for keyholder [SPR-10867] #15494

Closed
spring-projects-issues opened this issue Aug 28, 2013 · 1 comment
Closed

jdbctemplate for keyholder [SPR-10867] #15494

spring-projects-issues opened this issue Aug 28, 2013 · 1 comment
Labels
in: data status: bulk-closed

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 28, 2013

skzr.org opened SPR-10867 and commented

I hope JdbcTemplate can support more with KeyHolder.

  • updateRetKeyHolder like this:
public KeyHolder updateRetKeyHolder(final String sql, final Object... args) {
	logger.debug("Insert and return KeyHolder for SQL: {}", sql);
	KeyHolder keyHolder = new GeneratedKeyHolder();
	try {
		update(new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				setValues(ps, args);
				return ps;
			}
		}, keyHolder);
	} finally {
		StatementCreatorUtils.cleanupParameters(args);
	}
	return keyHolder;
}
  • batchUpdateRetKeyHolder like this:
public <T> List<KeyHolder> batchUpdateRetKeyHolder(JdbcTemplate jdbcTemplate, final String sql, final List<T> beans) {
	logger.debug("Batch update with KeyHolder for SQL: {}, batch size: {}", sql, beans.size());
	final List<KeyHolder> keyHolders = new ArrayList<>(beans.size());
	
	execute(new PreparedStatementCreator() {
		@Override
		public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
			return con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
		}
	}, new PreparedStatementCallback<int[]>() {
		@Override
		public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
			try {
				int batchSize = beans.size();
				int[] result;
				KeyHolder keyHolder = new GeneratedKeyHolder();
				
				try {
					if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
						for (int i = 0; i < batchSize; i++) {
							setValues(ps, i);
							ps.addBatch();
						}
						result = ps.executeBatch();
						
						generatedKeys(ps, keyHolder);
					} else {
						List<Integer> rowsAffected = new ArrayList<Integer>();
						for (int i = 0; i < batchSize; i++) {
							setValues(ps, i);
							
							rowsAffected.add(ps.executeUpdate());
							generatedKeys(ps, keyHolder);
						}
						
						result = UtilCollection.tointArray(rowsAffected);
					}
				} finally {
					keyHolders.add(keyHolder);
				}
				
				return result;
			} finally {
				cleanupParameters();
			}
		}
	});
	return keyHolders;
}

Affects: 3.2.4

@spring-projects-issues spring-projects-issues added status: waiting-for-triage in: data type: task and removed type: task labels Jan 11, 2019
@rstoyanchev rstoyanchev added status: bulk-closed and removed status: waiting-for-triage labels Jan 11, 2019
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 12, 2019

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data status: bulk-closed
Projects
None yet
Development

No branches or pull requests

2 participants