use of org.springframework.jdbc.core.ConnectionCallback in project uPortal by Jasig.
the class RDBMUserLayoutStore method getNextStructId.
/**
* Return the next available structure id for a user
*
* @return next free structure ID
*/
protected String getNextStructId(final IPerson person, final String prefix) {
final int userId = person.getID();
return nextStructTransactionOperations.execute(status -> jdbcOperations.execute(new ConnectionCallback<String>() {
@Override
public String doInConnection(Connection con) throws SQLException, DataAccessException {
try (Statement stmt = con.createStatement()) {
String sQuery = "SELECT NEXT_STRUCT_ID FROM UP_USER WHERE USER_ID=" + userId;
logger.debug("getNextStructId(): {}", sQuery);
int currentStructId;
try (ResultSet rs = stmt.executeQuery(sQuery)) {
if (rs.next()) {
currentStructId = rs.getInt(1);
} else {
throw new SQLException("no rows returned for query [" + sQuery + "]");
}
}
int nextStructId = currentStructId + 1;
String sUpdate = "UPDATE UP_USER SET NEXT_STRUCT_ID=" + nextStructId + " WHERE USER_ID=" + userId + " AND NEXT_STRUCT_ID=" + currentStructId;
logger.debug("getNextStructId(): {}", sUpdate);
stmt.executeUpdate(sUpdate);
return prefix + nextStructId;
}
}
}));
}
use of org.springframework.jdbc.core.ConnectionCallback in project spring-framework by spring-projects.
the class AbstractJdbcInsert method executeInsertAndReturnKeyHolderInternal.
/**
* Delegate method to execute the insert, generating any number of keys.
*/
private KeyHolder executeInsertAndReturnKeyHolderInternal(final List<?> values) {
if (logger.isDebugEnabled()) {
logger.debug("The following parameters are used for call " + getInsertString() + " with: " + values);
}
final KeyHolder keyHolder = new GeneratedKeyHolder();
if (this.tableMetaDataContext.isGetGeneratedKeysSupported()) {
getJdbcTemplate().update(con -> {
PreparedStatement ps = prepareStatementForGeneratedKeys(con);
setParameterValues(ps, values, getInsertTypes());
return ps;
}, keyHolder);
} else {
if (!this.tableMetaDataContext.isGetGeneratedKeysSimulated()) {
throw new InvalidDataAccessResourceUsageException("The getGeneratedKeys feature is not supported by this database");
}
if (getGeneratedKeyNames().length < 1) {
throw new InvalidDataAccessApiUsageException("Generated Key Name(s) not specified. " + "Using the generated keys features requires specifying the name(s) of the generated column(s)");
}
if (getGeneratedKeyNames().length > 1) {
throw new InvalidDataAccessApiUsageException("Current database only supports retrieving the key for a single column. There are " + getGeneratedKeyNames().length + " columns specified: " + Arrays.asList(getGeneratedKeyNames()));
}
Assert.state(getTableName() != null, "No table name set");
final String keyQuery = this.tableMetaDataContext.getSimpleQueryForGetGeneratedKey(getTableName(), getGeneratedKeyNames()[0]);
Assert.state(keyQuery != null, "Query for simulating get generated keys must not be null");
if (keyQuery.toUpperCase().startsWith("RETURNING")) {
Long key = getJdbcTemplate().queryForObject(getInsertString() + " " + keyQuery, Long.class, values.toArray());
Map<String, Object> keys = new HashMap<>(2);
keys.put(getGeneratedKeyNames()[0], key);
keyHolder.getKeyList().add(keys);
} else {
getJdbcTemplate().execute((ConnectionCallback<Object>) con -> {
PreparedStatement ps = null;
try {
ps = con.prepareStatement(getInsertString());
setParameterValues(ps, values, getInsertTypes());
ps.executeUpdate();
} finally {
JdbcUtils.closeStatement(ps);
}
Statement keyStmt = null;
ResultSet rs = null;
try {
keyStmt = con.createStatement();
rs = keyStmt.executeQuery(keyQuery);
if (rs.next()) {
long key = rs.getLong(1);
Map<String, Object> keys = new HashMap<>(2);
keys.put(getGeneratedKeyNames()[0], key);
keyHolder.getKeyList().add(keys);
}
} finally {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(keyStmt);
}
return null;
});
}
}
return keyHolder;
}
use of org.springframework.jdbc.core.ConnectionCallback in project spring-boot by spring-projects.
the class AbstractDataSourcePoolMetadataTests method getPoolSizeTwoConnections.
@Test
public void getPoolSizeTwoConnections() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
jdbcTemplate.execute(new ConnectionCallback<Void>() {
@Override
public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
assertThat(getDataSourceMetadata().getActive()).isEqualTo(2);
assertThat(getDataSourceMetadata().getUsage()).isEqualTo(1.0f);
return null;
}
});
return null;
}
});
}
use of org.springframework.jdbc.core.ConnectionCallback in project uPortal by Jasig.
the class RDBMLocaleStore method updateUserLocales.
@Override
public void updateUserLocales(final IPerson person, final Locale[] locales) {
this.transactionOperations.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
return jdbcOperations.execute(new ConnectionCallback<Object>() {
@Override
public Object doInConnection(Connection con) throws SQLException, DataAccessException {
// Delete the existing list of locales
final String delete = "DELETE FROM UP_USER_LOCALE WHERE USER_ID=?";
PreparedStatement pstmt = con.prepareStatement(delete);
try {
pstmt.clearParameters();
pstmt.setInt(1, person.getID());
logger.debug(delete);
pstmt.executeUpdate();
} finally {
pstmt.close();
}
// Insert the new list of locales
final String insert = "INSERT INTO UP_USER_LOCALE VALUES (?, ?, ?)";
pstmt = con.prepareStatement(insert);
try {
for (int i = 0; i < locales.length; i++) {
pstmt.clearParameters();
pstmt.setInt(1, person.getID());
pstmt.setString(2, locales[i].toString());
pstmt.setInt(3, i);
logger.debug(insert);
pstmt.executeUpdate();
}
} finally {
pstmt.close();
}
return null;
}
});
}
});
}
use of org.springframework.jdbc.core.ConnectionCallback in project spring-boot by spring-projects.
the class AbstractDataSourcePoolMetadataTests method getPoolSizeTwoConnections.
@Test
void getPoolSizeTwoConnections() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
jdbcTemplate.execute((ConnectionCallback<Void>) (connection) -> {
jdbcTemplate.execute((ConnectionCallback<Void>) (connection1) -> {
assertThat(getDataSourceMetadata().getActive()).isEqualTo(2);
assertThat(getDataSourceMetadata().getUsage()).isEqualTo(1.0f);
return null;
});
return null;
});
}
Aggregations