Search in sources :

Example 1 with DatabaseAccessor

use of org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor in project eclipselink by eclipse-ee4j.

the class TransactionIsolationLevelSwitchListener method postAcquireConnection.

@Override
public void postAcquireConnection(SessionEvent event) {
    Connection conn = ((DatabaseAccessor) event.getResult()).getConnection();
    Statement stmt1 = null;
    String isolationLevel = "";
    try {
        int i = conn.getTransactionIsolation();
        switch(i) {
            case 1:
                isolationLevel = "READ UNCOMMITTED";
                break;
            case 2:
                isolationLevel = "READ COMMITTED";
                break;
            case 4:
                isolationLevel = "REPEATABLE READ";
                break;
            case 8:
                isolationLevel = "SERIALIZABLE";
                break;
        }
        if (i > 0) {
            // If conn1 began transaction and updated the row (but hasn't
            // committed the transaction yet),
            // then conn2 should be able to read the original (not updated)
            // state of the row.
            // Without this conn2 reading the row hangs on Symfoware.
            stmt1 = conn.createStatement();
            stmt1.execute(statement + "READ UNCOMMITTED");
            connections.put(conn, isolationLevel);
        }
    } catch (SQLException sqlException) {
        throw new TestProblemException("postAcquireConnection failed. ", sqlException);
    } finally {
        if (stmt1 != null) {
            try {
                stmt1.close();
            } catch (SQLException ex) {
            // Ignore
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)

Example 2 with DatabaseAccessor

use of org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor in project eclipselink by eclipse-ee4j.

the class SybaseTransactionIsolationListener method isDatabaseVersionSupported.

// verify that it's version 15 or higher - this doesn't work with version 12.5
public static boolean isDatabaseVersionSupported(ServerSession serverSession) {
    DatabaseAccessor accessor = (DatabaseAccessor) serverSession.allocateReadConnection();
    int version;
    try {
        String strVersion = accessor.getConnectionMetaData().getDatabaseProductVersion();
        int iStart = strVersion.indexOf("/") + 1;
        int iEnd = strVersion.indexOf(".");
        String strIntVersion = strVersion.substring(iStart, iEnd);
        version = Integer.parseInt(strIntVersion);
    } catch (SQLException ex) {
        throw new TestProblemException("failed to obtain database version number", ex);
    }
    return version >= requiredVersion;
}
Also used : SQLException(java.sql.SQLException) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)

Example 3 with DatabaseAccessor

use of org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor in project eclipselink by eclipse-ee4j.

the class SybaseTransactionIsolationListener method postAcquireConnection.

@Override
public void postAcquireConnection(SessionEvent event) {
    Connection conn = ((DatabaseAccessor) event.getResult()).getConnection();
    Statement stmt1 = null;
    Statement stmt2 = null;
    ResultSet result = null;
    Integer isolationLevel;
    try {
        stmt1 = conn.createStatement();
        result = stmt1.executeQuery("select @@isolation");
        result.next();
        isolationLevel = result.getInt(1);
        if (isolationLevel > 0) {
            // If conn1 began transaction and updated the row (but hasn't committed the transaction yet),
            // then conn2 should be able to read the original (not updated) state of the row.
            // Without this conn2 reading the row hangs on Sybase.
            stmt2 = conn.createStatement();
            stmt2.execute("set transaction isolation level 0");
            stmt2.close();
            connections.put(conn, isolationLevel);
        }
    } catch (SQLException sqlException) {
        throw new TestProblemException("postAcquireConnection failed. ", sqlException);
    } finally {
        if (result != null) {
            try {
                result.close();
            } catch (SQLException ex) {
            // Ignore
            }
        }
        if (stmt1 != null) {
            try {
                stmt1.close();
            } catch (SQLException ex) {
            // Ignore
            }
        }
        if (stmt2 != null) {
            try {
                stmt2.close();
            } catch (SQLException ex) {
            // Ignore
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)

Example 4 with DatabaseAccessor

use of org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor in project eclipselink by eclipse-ee4j.

the class SybaseTransactionIsolationListener method preReleaseConnection.

@Override
public void preReleaseConnection(SessionEvent event) {
    Connection conn = ((DatabaseAccessor) event.getResult()).getConnection();
    Statement stmt = null;
    try {
        Integer isolationLevel = connections.remove(conn);
        if (isolationLevel != null) {
            // reset the original transaction isolation.
            stmt = conn.createStatement();
            stmt.execute("set transaction isolation level " + isolationLevel);
            stmt.close();
        }
    } catch (SQLException sqlException) {
        throw new TestProblemException("preReleaseConnection failed. ", sqlException);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ex) {
            // Ignore
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) Connection(java.sql.Connection) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)

Example 5 with DatabaseAccessor

use of org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor in project eclipselink by eclipse-ee4j.

the class ReadAllQuery method registerResultSetInUnitOfWork.

/**
 * INTERNAL:
 * Version of the previous method for ResultSet optimization.
 *
 * @return the final (conformed, refreshed, wrapped) UnitOfWork query result
 */
public Object registerResultSetInUnitOfWork(ResultSet resultSet, Vector fields, DatabaseField[] fieldsArray, UnitOfWorkImpl unitOfWork, AbstractRecord arguments) throws SQLException {
    // TODO: add support for Conforming results in UOW - currently conforming in uow is not compatible with ResultSet optimization.
    ContainerPolicy cp = this.containerPolicy;
    Object clones = cp.containerInstance();
    ResultSetMetaData metaData = resultSet.getMetaData();
    boolean hasNext = resultSet.next();
    if (hasNext) {
        // TODO: possibly add support for SortedListContainerPolicy (cp.shouldAddAll() == true) - this policy currently is not compatible with ResultSet optimization
        boolean quickAdd = (clones instanceof Collection) && !this.descriptor.getObjectBuilder().hasWrapperPolicy();
        DatabaseAccessor dbAccessor = (DatabaseAccessor) getAccessor();
        boolean useSimple = this.descriptor.getObjectBuilder().isSimple();
        AbstractSession executionSession = getExecutionSession();
        DatabasePlatform platform = dbAccessor.getPlatform();
        boolean optimizeData = platform.shouldOptimizeDataConversion();
        if (useSimple) {
            // None of the fields are relational - the row could be reused, just clear all the values.
            SimpleResultSetRecord row = new SimpleResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, platform, optimizeData);
            if (this.descriptor.isDescriptorTypeAggregate()) {
                // Aggregate Collection may have an unmapped primary key referencing the owner, the corresponding field will not be used when the object is populated and therefore may not be cleared.
                row.setShouldKeepValues(true);
            }
            while (hasNext) {
                Object clone = buildObject(row);
                if (quickAdd) {
                    ((Collection) clones).add(clone);
                } else {
                    // TODO: investigate is it possible to support MappedKeyMapPolicy - this policy currently is not compatible with ResultSet optimization
                    cp.addInto(clone, clones, unitOfWork);
                }
                row.reset();
                hasNext = resultSet.next();
            }
        } else {
            boolean shouldKeepRow = this.descriptor.getObjectBuilder().shouldKeepRow();
            while (hasNext) {
                ResultSetRecord row = new ResultSetRecord(fields, fieldsArray, resultSet, metaData, dbAccessor, executionSession, platform, optimizeData);
                Object clone = buildObject(row);
                if (quickAdd) {
                    ((Collection) clones).add(clone);
                } else {
                    // TODO: investigate is it possible to support MappedKeyMapPolicy - this policy currently is not compatible with ResultSet optimization
                    cp.addInto(clone, clones, unitOfWork);
                }
                if (shouldKeepRow) {
                    if (row.hasResultSet()) {
                        // ResultSet has not been fully triggered - that means the cached object was used.
                        // Yet the row still may be cached in a value holder (see loadBatchReadAttributes and loadJoinedAttributes methods).
                        // Remove ResultSet to avoid attempt to trigger it (already closed) when pk or fk values (already extracted) accessed when the value holder is instantiated.
                        row.removeResultSet();
                    } else {
                        row.removeNonIndirectionValues();
                    }
                }
                hasNext = resultSet.next();
            }
        }
    }
    return clones;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) Collection(java.util.Collection) InvalidObject(org.eclipse.persistence.internal.helper.InvalidObject) DatabaseAccessor(org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) ResultSetRecord(org.eclipse.persistence.internal.sessions.ResultSetRecord) DatabasePlatform(org.eclipse.persistence.internal.databaseaccess.DatabasePlatform) SimpleResultSetRecord(org.eclipse.persistence.internal.sessions.SimpleResultSetRecord) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

DatabaseAccessor (org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor)15 SQLException (java.sql.SQLException)10 Statement (java.sql.Statement)7 Connection (java.sql.Connection)5 ResultSet (java.sql.ResultSet)5 ResultSetMetaData (java.sql.ResultSetMetaData)5 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)5 DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)5 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)5 DatabaseCall (org.eclipse.persistence.internal.databaseaccess.DatabaseCall)4 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)4 DatasourceCallQueryMechanism (org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism)4 ResultSetRecord (org.eclipse.persistence.internal.sessions.ResultSetRecord)3 SimpleResultSetRecord (org.eclipse.persistence.internal.sessions.SimpleResultSetRecord)3 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)3 TestProblemException (org.eclipse.persistence.testing.framework.TestProblemException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 ThreadCursoredList (org.eclipse.persistence.internal.helper.ThreadCursoredList)2