Search in sources :

Example 1 with DataRetrievalFailureException

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;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) TypeMismatchException(org.springframework.beans.TypeMismatchException) ResultSetMetaData(java.sql.ResultSetMetaData) BeanWrapper(org.springframework.beans.BeanWrapper) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) HashSet(java.util.HashSet)

Example 2 with DataRetrievalFailureException

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();
}
Also used : LocalTransaction(javax.resource.cci.LocalTransaction) Interaction(javax.resource.cci.Interaction) InteractionSpec(javax.resource.cci.InteractionSpec) Connection(javax.resource.cci.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) CciLocalTransactionManager(org.springframework.jca.cci.connection.CciLocalTransactionManager) CciTemplate(org.springframework.jca.cci.core.CciTemplate) ResourceException(javax.resource.ResourceException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) ConnectionFactory(javax.resource.cci.ConnectionFactory) Record(javax.resource.cci.Record) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) Test(org.junit.Test)

Example 3 with DataRetrievalFailureException

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);
}
Also used : CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException)

Example 4 with DataRetrievalFailureException

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);
    }
}
Also used : Ticket(org.opennms.api.integration.ticketing.Ticket) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) Properties(java.util.Properties)

Example 5 with DataRetrievalFailureException

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);
    }
}
Also used : EnterpriseIdPartition(org.opennms.netmgt.xml.eventconf.EnterpriseIdPartition) InputStreamReader(java.io.InputStreamReader) EventOrdering(org.opennms.netmgt.xml.eventconf.EventOrdering) Events(org.opennms.netmgt.xml.eventconf.Events) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException)

Aggregations

DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)14 IOException (java.io.IOException)3 Ticket (org.opennms.api.integration.ticketing.Ticket)3 EventOrdering (org.opennms.netmgt.xml.eventconf.EventOrdering)3 Events (org.opennms.netmgt.xml.eventconf.Events)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)2 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)2 DataRecord (org.aspcfs.apps.transfer.DataRecord)2 DataAccessException (org.springframework.dao.DataAccessException)2 QuickBaseClient (com.intuit.quickbase.util.QuickBaseClient)1 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ResultSetMetaData (java.sql.ResultSetMetaData)1