Search in sources :

Example 31 with SQLException

use of java.sql.SQLException in project hibernate-orm by hibernate.

the class JdbcTimestampCustomSessionLevelTimeZoneTest method testTimeZone.

@Test
public void testTimeZone() {
    connectionProvider.clear();
    doInHibernateSessionBuilder(() -> {
        return sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE);
    }, s -> {
        Person person = new Person();
        person.id = 1L;
        s.persist(person);
    });
    assertEquals(1, connectionProvider.getPreparedStatements().size());
    PreparedStatement ps = connectionProvider.getPreparedStatements().get(0);
    try {
        ArgumentCaptor<Calendar> calendarArgumentCaptor = ArgumentCaptor.forClass(Calendar.class);
        verify(ps, times(1)).setTimestamp(anyInt(), any(Timestamp.class), calendarArgumentCaptor.capture());
        assertEquals(TIME_ZONE, calendarArgumentCaptor.getValue().getTimeZone());
    } catch (SQLException e) {
        fail(e.getMessage());
    }
    connectionProvider.clear();
    doInHibernateSessionBuilder(() -> {
        return sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE);
    }, s -> {
        s.doWork(connection -> {
            try (Statement st = connection.createStatement()) {
                try (ResultSet rs = st.executeQuery("select createdOn from Person")) {
                    while (rs.next()) {
                        Timestamp timestamp = rs.getTimestamp(1);
                        int offsetDiff = TimeZone.getDefault().getOffset(0) - TIME_ZONE.getOffset(0);
                        assertEquals(Math.abs(Long.valueOf(offsetDiff).longValue()), Math.abs(timestamp.getTime()));
                    }
                }
            }
        });
        Person person = s.find(Person.class, 1L);
        assertEquals(0, person.createdOn.getTime());
    });
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Calendar(java.util.Calendar) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 32 with SQLException

use of java.sql.SQLException in project hibernate-orm by hibernate.

the class XaTransactionImpl method commit.

public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException {
    if (status == Status.STATUS_MARKED_ROLLBACK) {
        log.trace("on commit, status was marked for rollback-only");
        rollback();
    } else {
        status = Status.STATUS_PREPARING;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.beforeCompletion();
            }
        }
        if (!runXaResourcePrepare()) {
            status = Status.STATUS_ROLLING_BACK;
        } else {
            status = Status.STATUS_PREPARED;
        }
        status = Status.STATUS_COMMITTING;
        if (connection != null) {
            try {
                connection.commit();
                connectionProvider.closeConnection(connection);
                connection = null;
            } catch (SQLException sqle) {
                status = Status.STATUS_UNKNOWN;
                throw new SystemException();
            }
        }
        runXaResourceCommitTx();
        status = Status.STATUS_COMMITTED;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.afterCompletion(status);
            }
        }
        // status = Status.STATUS_NO_TRANSACTION;
        jtaTransactionManager.endCurrent(this);
    }
}
Also used : SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization)

Example 33 with SQLException

use of java.sql.SQLException in project hibernate-orm by hibernate.

the class DualNodeJtaTransactionImpl method commit.

public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException {
    if (status == Status.STATUS_MARKED_ROLLBACK) {
        log.trace("on commit, status was marked for rollback-only");
        rollback();
    } else {
        status = Status.STATUS_PREPARING;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.beforeCompletion();
            }
        }
        if (!runXaResourcePrepare()) {
            status = Status.STATUS_ROLLING_BACK;
        } else {
            status = Status.STATUS_PREPARED;
        }
        status = Status.STATUS_COMMITTING;
        if (connection != null) {
            try {
                connection.commit();
                connection.close();
            } catch (SQLException sqle) {
                status = Status.STATUS_UNKNOWN;
                throw new SystemException();
            }
        }
        runXaResourceCommitTx();
        status = Status.STATUS_COMMITTED;
        if (synchronizations != null) {
            for (int i = 0; i < synchronizations.size(); i++) {
                Synchronization s = (Synchronization) synchronizations.get(i);
                s.afterCompletion(status);
            }
        }
        // status = Status.STATUS_NO_TRANSACTION;
        jtaTransactionManager.endCurrent(this);
    }
}
Also used : SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization)

Example 34 with SQLException

use of java.sql.SQLException in project hibernate-orm by hibernate.

the class DualNodeJtaTransactionImpl method rollback.

public void rollback() throws IllegalStateException, SystemException {
    status = Status.STATUS_ROLLING_BACK;
    runXaResourceRollback();
    status = Status.STATUS_ROLLEDBACK;
    if (connection != null) {
        try {
            connection.rollback();
            connection.close();
        } catch (SQLException sqle) {
            status = Status.STATUS_UNKNOWN;
            throw new SystemException();
        }
    }
    if (synchronizations != null) {
        for (int i = 0; i < synchronizations.size(); i++) {
            Synchronization s = (Synchronization) synchronizations.get(i);
            s.afterCompletion(status);
        }
    }
    // status = Status.STATUS_NO_TRANSACTION;
    jtaTransactionManager.endCurrent(this);
}
Also used : SystemException(javax.transaction.SystemException) SQLException(java.sql.SQLException) Synchronization(javax.transaction.Synchronization)

Example 35 with SQLException

use of java.sql.SQLException in project Openfire by igniterealtime.

the class DbRuleManager method getRuleByOrderId.

public Rule getRuleByOrderId(int order) {
    Rule rule = null;
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(GET_RULE_BY_ORDER_ID);
        pstmt.setInt(1, order);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            rule.setOrder(rs.getInt(1));
            String ruleType = rs.getString(2);
            if (ruleType.equals(Reject.class.getName())) {
                rule = new Reject();
            } else if (ruleType.equals(Pass.class.getName())) {
                rule = new Pass();
            } else if (ruleType.equals(Drop.class.getName())) {
                rule = new Drop();
            }
            rule.setRuleId(rs.getString(3));
            rule.setPacketType(Rule.PacketType.valueOf(rs.getString(4)));
            rule.setDestination(rs.getString(5));
            rule.setSource(rs.getString(6));
            rule.isDisabled(rs.getBoolean(7));
            rule.doLog(rs.getBoolean(8));
            rule.setDescription(rs.getString(9));
            rule.setSourceType(Rule.SourceDestType.valueOf(rs.getString(10)));
            rule.setDestType(Rule.SourceDestType.valueOf(rs.getString(11)));
        }
    } catch (SQLException sqle) {
        Log.error(sqle.getMessage(), sqle);
    } finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
    return rule;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

SQLException (java.sql.SQLException)17708 PreparedStatement (java.sql.PreparedStatement)7646 ResultSet (java.sql.ResultSet)6472 Connection (java.sql.Connection)6226 Statement (java.sql.Statement)3089 ArrayList (java.util.ArrayList)2495 Test (org.junit.Test)1525 IOException (java.io.IOException)1220 List (java.util.List)760 HashMap (java.util.HashMap)633 CallableStatement (java.sql.CallableStatement)506 Map (java.util.Map)471 Properties (java.util.Properties)426 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)387 Timestamp (java.sql.Timestamp)363 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)328 DatabaseMetaData (java.sql.DatabaseMetaData)326 ResultSetMetaData (java.sql.ResultSetMetaData)301 DataSource (javax.sql.DataSource)295 File (java.io.File)280