use of org.springframework.dao.DataRetrievalFailureException in project spring-framework by spring-projects.
the class BeanPropertyRowMapper method mapRow.
/**
* Extract the values for all columns in the current row.
* <p>Utilizes public setters and result set metadata.
* @see java.sql.ResultSetMetaData
*/
@Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
Assert.state(this.mappedClass != null, "Mapped class was not specified");
T mappedObject = BeanUtils.instantiateClass(this.mappedClass);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
initBeanWrapper(bw);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<>() : null);
for (int index = 1; index <= columnCount; index++) {
String column = JdbcUtils.lookupColumnName(rsmd, index);
String field = lowerCaseName(column.replaceAll(" ", ""));
PropertyDescriptor pd = this.mappedFields.get(field);
if (pd != null) {
try {
Object value = getColumnValue(rs, index, pd);
if (rowNumber == 0 && logger.isDebugEnabled()) {
logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "'");
}
try {
bw.setPropertyValue(pd.getName(), value);
} catch (TypeMismatchException ex) {
if (value == null && this.primitivesDefaultedForNullValue) {
if (logger.isDebugEnabled()) {
logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with null value when setting property '" + pd.getName() + "' of type '" + ClassUtils.getQualifiedName(pd.getPropertyType()) + "' on object: " + mappedObject, ex);
}
} else {
throw ex;
}
}
if (populatedProperties != null) {
populatedProperties.add(pd.getName());
}
} catch (NotWritablePropertyException ex) {
throw new DataRetrievalFailureException("Unable to map column '" + column + "' to property '" + pd.getName() + "'", ex);
}
} else {
// No PropertyDescriptor found
if (rowNumber == 0 && logger.isDebugEnabled()) {
logger.debug("No property found for column '" + column + "' mapped to field '" + field + "'");
}
}
}
if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) {
throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass.getName() + "]: " + this.mappedProperties);
}
return mappedObject;
}
use of org.springframework.dao.DataRetrievalFailureException in project spring-framework by spring-projects.
the class CciLocalTransactionTests method testLocalTransactionRollback.
/**
* Test if a transaction ( begin / rollback ) is executed on the
* LocalTransaction when CciLocalTransactionManager is specified as
* transaction manager and a non-checked exception is thrown.
*/
@Test
public void testLocalTransactionRollback() throws ResourceException {
final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
Connection connection = mock(Connection.class);
Interaction interaction = mock(Interaction.class);
LocalTransaction localTransaction = mock(LocalTransaction.class);
final Record record = mock(Record.class);
final InteractionSpec interactionSpec = mock(InteractionSpec.class);
given(connectionFactory.getConnection()).willReturn(connection);
given(connection.getLocalTransaction()).willReturn(localTransaction);
given(connection.createInteraction()).willReturn(interaction);
given(interaction.execute(interactionSpec, record, record)).willReturn(true);
given(connection.getLocalTransaction()).willReturn(localTransaction);
CciLocalTransactionManager tm = new CciLocalTransactionManager();
tm.setConnectionFactory(connectionFactory);
TransactionTemplate tt = new TransactionTemplate(tm);
try {
tt.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory));
CciTemplate ct = new CciTemplate(connectionFactory);
ct.execute(interactionSpec, record, record);
throw new DataRetrievalFailureException("error");
}
});
} catch (Exception ex) {
}
verify(localTransaction).begin();
verify(interaction).close();
verify(localTransaction).rollback();
verify(connection).close();
}
use of org.springframework.dao.DataRetrievalFailureException in project gocd by gocd.
the class StageSqlMapDao method stageById.
public Stage stageById(long id) {
String key = cacheKeyForStageById(id);
Stage stage = (Stage) goCache.get(key);
if (stage == null) {
synchronized (key) {
stage = (Stage) goCache.get(key);
if (stage == null) {
stage = (Stage) getSqlMapClientTemplate().queryForObject("getStageById", id);
if (stage == null) {
throw new DataRetrievalFailureException("Unable to load related stage data for id " + id);
}
goCache.put(key, stage);
}
}
}
return cloner.deepClone(stage);
}
use of org.springframework.dao.DataRetrievalFailureException in project opennms by OpenNMS.
the class QuickBaseTicketerPlugin method get.
public Ticket get(String ticketId) {
try {
Properties props = getProperties();
MyQuickBaseClient qdb = createClient(getUserName(props), getPassword(props), getUrl(props));
String dbId = qdb.findDbByName(getApplicationName(props));
HashMap<String, String> record = qdb.getRecordInfo(dbId, ticketId);
Ticket ticket = new Ticket();
ticket.setId(ticketId);
ticket.setModificationTimestamp(record.get(getModificationTimeStampFile(props)));
ticket.setSummary(record.get(getSummaryField(props)));
ticket.setDetails(record.get(getDetailsField(props)));
ticket.setState(getTicketStateValue(record.get(getStateField(props)), props));
return ticket;
} catch (Throwable e) {
throw new DataRetrievalFailureException("Failed to commit QuickBase transaction: " + e.getMessage(), e);
}
}
use of org.springframework.dao.DataRetrievalFailureException in project opennms by OpenNMS.
the class MockEventConfDao method reload.
@Override
public void reload() throws DataAccessException {
InputStream is = null;
InputStreamReader isr = null;
try {
is = m_resource.getInputStream();
isr = new InputStreamReader(is);
final Reader reader = isr;
m_events = JaxbUtils.unmarshal(Events.class, reader);
m_events.loadEventFiles(m_resource);
m_events.initialize(new EnterpriseIdPartition(), new EventOrdering());
} catch (final IOException e) {
throw new DataRetrievalFailureException("Failed to read from " + m_resource.toString(), e);
} finally {
IOUtils.closeQuietly(isr);
IOUtils.closeQuietly(is);
}
}
Aggregations