Search in sources :

Example 91 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-boot by spring-projects.

the class AbstractDataSourcePoolMetadataTests method getPoolSizeNoConnection.

@Test
public void getPoolSizeNoConnection() {
    // Make sure the pool is initialized
    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
            return null;
        }
    });
    assertThat(getDataSourceMetadata().getActive()).isEqualTo(Integer.valueOf(0));
    assertThat(getDataSourceMetadata().getUsage()).isEqualTo(Float.valueOf(0));
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 92 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-boot by spring-projects.

the class AbstractDataSourcePoolMetadataTests method getPoolSizeOneConnection.

@Test
public void getPoolSizeOneConnection() {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
            assertThat(getDataSourceMetadata().getActive()).isEqualTo(Integer.valueOf(1));
            assertThat(getDataSourceMetadata().getUsage()).isEqualTo(Float.valueOf(0.5F));
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 93 with DataAccessException

use of org.springframework.dao.DataAccessException in project opennms by OpenNMS.

the class DefaultRrdGraphService method getInputStreamForCommand.

private InputStream getInputStreamForCommand(String command) {
    boolean debug = true;
    InputStream tempIn = null;
    try {
        LOG.debug("Executing RRD command: {}", command);
        tempIn = m_rrdDao.createGraph(command);
    } catch (final DataAccessException e) {
        LOG.warn("Exception while creating graph.", e);
        if (debug) {
            throw e;
        } else {
            return returnErrorImage(s_rrdError);
        }
    }
    return tempIn;
}
Also used : InputStream(java.io.InputStream) DataAccessException(org.springframework.dao.DataAccessException)

Example 94 with DataAccessException

use of org.springframework.dao.DataAccessException in project opennms by OpenNMS.

the class UdpUuidSender method run.

/**
     * <p>run</p>
     */
@Override
public void run() {
    // get the context
    m_context = Thread.currentThread();
    Logging.putPrefix(m_logPrefix);
    if (m_stop) {
        LOG.debug("Stop flag set before thread started, exiting");
        return;
    } else {
        LOG.debug("Thread context started");
    }
    /*
         * This loop is labeled so that it can be
         * exited quickly when the thread is interrupted.
         */
    List<UdpReceivedEvent> eventHold = new ArrayList<UdpReceivedEvent>(30);
    Map<UdpReceivedEvent, EventReceipt> receipts = new HashMap<UdpReceivedEvent, EventReceipt>();
    RunLoop: while (!m_stop) {
        LOG.debug("Waiting on event receipts to be generated");
        synchronized (m_eventUuidsOut) {
            // wait for an event to show up.  wait in 1 second intervals
            while (m_eventUuidsOut.isEmpty()) {
                try {
                    // use wait instead of sleep to release the lock!
                    m_eventUuidsOut.wait(1000);
                } catch (InterruptedException ie) {
                    LOG.debug("Thread context interrupted");
                    break RunLoop;
                }
            }
            eventHold.addAll(m_eventUuidsOut);
            m_eventUuidsOut.clear();
        }
        LOG.debug("Received {} event receipts to process", eventHold.size());
        LOG.debug("Processing receipts");
        // build an event-receipt
        for (UdpReceivedEvent re : eventHold) {
            for (Event e : re.getAckedEvents()) {
                if (e.getUuid() != null) {
                    EventReceipt receipt = receipts.get(re);
                    if (receipt == null) {
                        receipt = new EventReceipt();
                        receipts.put(re, receipt);
                    }
                    receipt.addUuid(e.getUuid());
                }
            }
        }
        eventHold.clear();
        LOG.debug("Event receipts sorted, transmitting receipts");
        // turn them into XML and send it out the socket
        for (Map.Entry<UdpReceivedEvent, EventReceipt> entry : receipts.entrySet()) {
            UdpReceivedEvent re = entry.getKey();
            EventReceipt receipt = entry.getValue();
            StringWriter writer = new StringWriter();
            try {
                JaxbUtils.marshal(receipt, writer);
            } catch (DataAccessException e) {
                LOG.warn("Failed to build event receipt for agent {}:{}", InetAddressUtils.str(re.getSender()), re.getPort(), e);
            }
            String xml = writer.getBuffer().toString();
            try {
                byte[] xml_bytes = xml.getBytes(StandardCharsets.US_ASCII);
                DatagramPacket pkt = new DatagramPacket(xml_bytes, xml_bytes.length, re.getSender(), re.getPort());
                LOG.debug("Transmitting receipt to destination {}:{}", InetAddressUtils.str(re.getSender()), re.getPort());
                m_dgSock.send(pkt);
                synchronized (m_handlers) {
                    for (EventHandler handler : m_handlers) {
                        try {
                            handler.receiptSent(receipt);
                        } catch (Throwable t) {
                            LOG.warn("Error processing event receipt", t);
                        }
                    }
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Receipt transmitted OK {");
                    LOG.debug(xml);
                    LOG.debug("}");
                }
            } catch (IOException e) {
                LOG.warn("Failed to send packet to host {}:{}", InetAddressUtils.str(re.getSender()), re.getPort(), e);
            }
        }
        receipts.clear();
    }
    LOG.debug("Context finished, returning");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EventHandler(org.opennms.netmgt.eventd.adaptors.EventHandler) IOException(java.io.IOException) EventReceipt(org.opennms.netmgt.xml.event.EventReceipt) StringWriter(java.io.StringWriter) DatagramPacket(java.net.DatagramPacket) Event(org.opennms.netmgt.xml.event.Event) DataAccessException(org.springframework.dao.DataAccessException)

Example 95 with DataAccessException

use of org.springframework.dao.DataAccessException in project alf.io by alfio-event.

the class SpecialPriceTokenGenerator method generateCode.

private void generateCode(SpecialPrice specialPrice) {
    TicketCategory ticketCategory = ticketCategoryRepository.getByIdAndActive(specialPrice.getTicketCategoryId()).orElseThrow(IllegalStateException::new);
    Event event = eventRepository.findById(ticketCategory.getEventId());
    int maxLength = configurationManager.getIntConfigValue(Configuration.from(event.getOrganizationId(), event.getId(), ticketCategory.getId(), ConfigurationKeys.SPECIAL_PRICE_CODE_LENGTH), 6);
    while (true) {
        try {
            log.trace("generate code for special price with id {}", specialPrice.getId());
            specialPriceRepository.updateCode(nextValidCode(maxLength), specialPrice.getId());
            log.trace("done.");
            return;
        } catch (DataAccessException e) {
            log.warn("got a duplicate. Retrying...", e);
        }
    }
}
Also used : Event(alfio.model.Event) TicketCategory(alfio.model.TicketCategory) DataAccessException(org.springframework.dao.DataAccessException)

Aggregations

DataAccessException (org.springframework.dao.DataAccessException)154 SQLException (java.sql.SQLException)50 ResultSet (java.sql.ResultSet)22 Connection (java.sql.Connection)18 PreparedStatement (java.sql.PreparedStatement)17 Test (org.junit.jupiter.api.Test)17 MongoException (com.mongodb.MongoException)14 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 List (java.util.List)13 HashMap (java.util.HashMap)11 Map (java.util.Map)11 Transactional (org.springframework.transaction.annotation.Transactional)11 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)10 Document (org.bson.Document)9 Test (org.junit.Test)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 RedisConnection (org.springframework.data.redis.connection.RedisConnection)7 RedisCallback (org.springframework.data.redis.core.RedisCallback)7