Search in sources :

Example 1 with SQLEntityState

use of org.qi4j.entitystore.sql.internal.SQLEntityState in project qi4j-sdk by Qi4j.

the class SQLEntityStoreMixin method applyChanges.

@Override
public StateCommitter applyChanges(final EntityStoreUnitOfWork unitofwork, final Iterable<EntityState> states) {
    return new StateCommitter() {

        @Override
        public void commit() {
            Connection connection = null;
            PreparedStatement insertPS = null;
            PreparedStatement updatePS = null;
            PreparedStatement removePS = null;
            try {
                connection = database.getConnection();
                connection.setAutoCommit(false);
                insertPS = database.prepareInsertEntityStatement(connection);
                updatePS = database.prepareUpdateEntityStatement(connection);
                removePS = database.prepareRemoveEntityStatement(connection);
                for (EntityState state : states) {
                    EntityStatus status = state.status();
                    DefaultEntityState defState = ((SQLEntityState) state).getDefaultEntityState();
                    Long entityPK = ((SQLEntityState) state).getEntityPK();
                    if (EntityStatus.REMOVED.equals(status)) {
                        database.populateRemoveEntityStatement(removePS, entityPK, state.identity());
                        removePS.addBatch();
                    } else {
                        StringWriter writer = new StringWriter();
                        writeEntityState(defState, writer, unitofwork.identity());
                        writer.flush();
                        if (EntityStatus.UPDATED.equals(status)) {
                            Long entityOptimisticLock = ((SQLEntityState) state).getEntityOptimisticLock();
                            database.populateUpdateEntityStatement(updatePS, entityPK, entityOptimisticLock, defState.identity(), writer.toString(), unitofwork.currentTime());
                            updatePS.addBatch();
                        } else if (EntityStatus.NEW.equals(status)) {
                            database.populateInsertEntityStatement(insertPS, defState.identity(), writer.toString(), unitofwork.currentTime());
                            insertPS.addBatch();
                        }
                    }
                }
                removePS.executeBatch();
                insertPS.executeBatch();
                updatePS.executeBatch();
                connection.commit();
            } catch (SQLException sqle) {
                SQLUtil.rollbackQuietly(connection);
                if (LOGGER.isDebugEnabled()) {
                    StringWriter sb = new StringWriter();
                    sb.append("SQLException during commit, logging nested exceptions before throwing EntityStoreException:\n");
                    SQLException e = sqle;
                    while (e != null) {
                        e.printStackTrace(new PrintWriter(sb, true));
                        e = e.getNextException();
                    }
                    LOGGER.debug(sb.toString());
                }
                throw new EntityStoreException(sqle);
            } catch (RuntimeException re) {
                SQLUtil.rollbackQuietly(connection);
                throw new EntityStoreException(re);
            } finally {
                SQLUtil.closeQuietly(insertPS);
                SQLUtil.closeQuietly(updatePS);
                SQLUtil.closeQuietly(removePS);
                SQLUtil.closeQuietly(connection);
            }
        }

        @Override
        public void cancel() {
        }
    };
}
Also used : SQLEntityState(org.qi4j.entitystore.sql.internal.SQLEntityState) DefaultSQLEntityState(org.qi4j.entitystore.sql.internal.SQLEntityState.DefaultSQLEntityState) DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLEntityState(org.qi4j.entitystore.sql.internal.SQLEntityState) EntityState(org.qi4j.spi.entity.EntityState) DefaultSQLEntityState(org.qi4j.entitystore.sql.internal.SQLEntityState.DefaultSQLEntityState) DefaultEntityState(org.qi4j.spi.entitystore.helpers.DefaultEntityState) StringWriter(java.io.StringWriter) EntityStatus(org.qi4j.spi.entity.EntityStatus) StateCommitter(org.qi4j.spi.entitystore.StateCommitter) EntityStoreException(org.qi4j.spi.entitystore.EntityStoreException) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 SQLEntityState (org.qi4j.entitystore.sql.internal.SQLEntityState)1 DefaultSQLEntityState (org.qi4j.entitystore.sql.internal.SQLEntityState.DefaultSQLEntityState)1 EntityState (org.qi4j.spi.entity.EntityState)1 EntityStatus (org.qi4j.spi.entity.EntityStatus)1 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)1 StateCommitter (org.qi4j.spi.entitystore.StateCommitter)1 DefaultEntityState (org.qi4j.spi.entitystore.helpers.DefaultEntityState)1