Search in sources :

Example 46 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project ma-core-public by infiniteautomation.

the class EventHandlerDao method saveEventHandler.

private AbstractEventHandlerVO<?> saveEventHandler(final String typeName, final String subtypeName, final int typeRef1, final int typeRef2, final AbstractEventHandlerVO<?> handler) {
    getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            if (handler.getId() == Common.NEW_ID)
                insertEventHandler(typeName, subtypeName, typeRef1, typeRef2, handler);
            else
                updateEventHandler(handler);
            if (handler.getAddedEventTypes() != null)
                for (EventType et : handler.getAddedEventTypes()) EventHandlerDao.instance.addEventHandlerMappingIfMissing(handler.getId(), et);
        }
    });
    return getEventHandler(handler.getId());
}
Also used : AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 47 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project neo-java by coranos.

the class BlockDbH2Impl method deleteBlockAtHeight.

/**
 * used to get blocks unstuck, during debugging.
 *
 * @param blockHeight
 *            the block height to remove.
 */
public void deleteBlockAtHeight(final long blockHeight) {
    final byte[] blockHeightBa = BlockUtil.getBlockHeightBa(blockHeight);
    final DataSourceTransactionManager tsMan = new DataSourceTransactionManager(ds);
    final TransactionTemplate txTemplate = new TransactionTemplate(tsMan);
    txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    try {
        txTemplate.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(final TransactionStatus ts) {
                try {
                    final JdbcTemplate t = new JdbcTemplate(ds);
                    executeSqlGroup(t, "deleteBlockAtHeight", blockHeightBa);
                } catch (final Exception e) {
                    if (LOG.isErrorEnabled()) {
                        LOG.error("deleteBlockAtHeight sql exception", e);
                    }
                }
            }
        });
    } catch (final DataAccessException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("deleteBlockAtHeight data access exception", e);
        }
    }
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) NotImplementedException(org.apache.commons.lang3.NotImplementedException) DataAccessException(org.springframework.dao.DataAccessException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DataAccessException(org.springframework.dao.DataAccessException)

Example 48 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project collect by openforis.

the class CollectRDBMonitor method runInTransaction.

private void runInTransaction(final Runnable task) {
    TransactionTemplate tmpl = new TransactionTemplate(transactionManager);
    tmpl.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            task.run();
        }
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 49 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-session by spring-projects.

the class JdbcOperationsSessionRepository method save.

@Override
public void save(final JdbcSession session) {
    if (session.isNew()) {
        this.transactionOperations.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                JdbcOperationsSessionRepository.this.jdbcOperations.update(JdbcOperationsSessionRepository.this.createSessionQuery, ps -> {
                    ps.setString(1, session.primaryKey);
                    ps.setString(2, session.getId());
                    ps.setLong(3, session.getCreationTime().toEpochMilli());
                    ps.setLong(4, session.getLastAccessedTime().toEpochMilli());
                    ps.setInt(5, (int) session.getMaxInactiveInterval().getSeconds());
                    ps.setLong(6, session.getExpiryTime().toEpochMilli());
                    ps.setString(7, session.getPrincipalName());
                });
                if (!session.getAttributeNames().isEmpty()) {
                    final List<String> attributeNames = new ArrayList<>(session.getAttributeNames());
                    JdbcOperationsSessionRepository.this.jdbcOperations.batchUpdate(JdbcOperationsSessionRepository.this.createSessionAttributeQuery, new BatchPreparedStatementSetter() {

                        @Override
                        public void setValues(PreparedStatement ps, int i) throws SQLException {
                            String attributeName = attributeNames.get(i);
                            ps.setString(1, session.primaryKey);
                            ps.setString(2, attributeName);
                            serialize(ps, 3, session.getAttribute(attributeName));
                        }

                        @Override
                        public int getBatchSize() {
                            return attributeNames.size();
                        }
                    });
                }
            }
        });
    } else {
        this.transactionOperations.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                if (session.isChanged()) {
                    JdbcOperationsSessionRepository.this.jdbcOperations.update(JdbcOperationsSessionRepository.this.updateSessionQuery, ps -> {
                        ps.setString(1, session.getId());
                        ps.setLong(2, session.getLastAccessedTime().toEpochMilli());
                        ps.setInt(3, (int) session.getMaxInactiveInterval().getSeconds());
                        ps.setLong(4, session.getExpiryTime().toEpochMilli());
                        ps.setString(5, session.getPrincipalName());
                        ps.setString(6, session.primaryKey);
                    });
                }
                Map<String, Object> delta = session.getDelta();
                if (!delta.isEmpty()) {
                    for (final Map.Entry<String, Object> entry : delta.entrySet()) {
                        if (entry.getValue() == null) {
                            JdbcOperationsSessionRepository.this.jdbcOperations.update(JdbcOperationsSessionRepository.this.deleteSessionAttributeQuery, ps -> {
                                ps.setString(1, session.primaryKey);
                                ps.setString(2, entry.getKey());
                            });
                        } else {
                            int updatedCount = JdbcOperationsSessionRepository.this.jdbcOperations.update(JdbcOperationsSessionRepository.this.updateSessionAttributeQuery, ps -> {
                                serialize(ps, 1, entry.getValue());
                                ps.setString(2, session.primaryKey);
                                ps.setString(3, entry.getKey());
                            });
                            if (updatedCount == 0) {
                                JdbcOperationsSessionRepository.this.jdbcOperations.update(JdbcOperationsSessionRepository.this.createSessionAttributeQuery, ps -> {
                                    ps.setString(1, session.primaryKey);
                                    ps.setString(2, entry.getKey());
                                    serialize(ps, 3, entry.getValue());
                                });
                            }
                        }
                    }
                }
            }
        });
    }
    session.clearChangeFlags();
}
Also used : BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) DataAccessException(org.springframework.dao.DataAccessException) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) TransactionDefinition(org.springframework.transaction.TransactionDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) LobHandler(org.springframework.jdbc.support.lob.LobHandler) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Duration(java.time.Duration) Map(java.util.Map) DeserializingConverter(org.springframework.core.serializer.support.DeserializingConverter) DefaultLobHandler(org.springframework.jdbc.support.lob.DefaultLobHandler) FindByIndexNameSessionRepository(org.springframework.session.FindByIndexNameSessionRepository) ConversionService(org.springframework.core.convert.ConversionService) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Set(java.util.Set) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Session(org.springframework.session.Session) UUID(java.util.UUID) PreparedStatement(java.sql.PreparedStatement) Instant(java.time.Instant) SerializingConverter(org.springframework.core.serializer.support.SerializingConverter) List(java.util.List) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) TransactionOperations(org.springframework.transaction.support.TransactionOperations) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Expression(org.springframework.expression.Expression) TransactionStatus(org.springframework.transaction.TransactionStatus) Log(org.apache.commons.logging.Log) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LogFactory(org.apache.commons.logging.LogFactory) MapSession(org.springframework.session.MapSession) Collections(java.util.Collections) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) TransactionStatus(org.springframework.transaction.TransactionStatus) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 50 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project ma-modules-public by infiniteautomation.

the class ScheduledEventDao method deleteScheduledEvent.

public void deleteScheduledEvent(final int scheduledEventId) {
    ScheduledEventVO se = getScheduledEvent(scheduledEventId);
    final ExtendedJdbcTemplate ejt2 = ejt;
    if (se != null) {
        getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                ejt2.update("delete from eventHandlersMapping where eventTypeName=? and eventTypeRef1=?", new Object[] { ScheduledEventType.TYPE_NAME, scheduledEventId });
                ejt2.update("delete from scheduledEvents where id=?", new Object[] { scheduledEventId });
            }
        });
        AuditEventType.raiseDeletedEvent(AuditEvent.TYPE_NAME, se);
        this.countMonitor.decrement();
    }
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)328 TransactionStatus (org.springframework.transaction.TransactionStatus)277 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)179 Test (org.junit.jupiter.api.Test)158 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)70 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)54 UserTransaction (jakarta.transaction.UserTransaction)43 SQLException (java.sql.SQLException)37 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)29 Test (org.junit.Test)25 Connection (java.sql.Connection)23 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)22 InOrder (org.mockito.InOrder)21 Date (java.util.Date)18 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)18 List (java.util.List)16 DataSource (javax.sql.DataSource)15 ArrayList (java.util.ArrayList)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13