Skip to content

EclipseLink does not support read-only database connections [SPR-7891] #12547

Closed

Description

@spring-projects-issues

Florian Feigenbutz opened SPR-7891 and commented

We are using Spring and EclipseLink to establish two separated connection pools to replicated MySQL systems.

One connection pool contains "write" connections and should interact with the MySQL master only. The other connection pool contains "read" connections addressing the MySQL slaves.
These pools are established using EclipseLink's connection pool implementation and the MySQL replication driver.

This driver decides whether to address the MySQL master or slave host(s) based on the connection's "readOnly" attribute. Connections having "readOnly=true" will be executed on any of the configured slaves while "readOnly=false" will address the MySQL master system.

We use the Transactional annotation in order to control whether our methods should make use of read or write connections, e.g.:
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)

Unfortunately the annotation's "readOnly" attribute is never transformed into read-only database connections in our environment.
It seems as if the EclipseLinkJpaDialect is missing to set this attribute in the getJdbcConnection method:

@Override
public ConnectionHandle getJdbcConnection(EntityManager em, boolean readOnly)
    throws PersistenceException, SQLException {
    AbstractSession session = (AbstractSession) getSession(em);
    // The connection was already acquired eagerly in beginTransaction,
    // unless lazyDatabaseTransaction was set to true.
    Connection con = session.getAccessor().getConnection();
    return (con != null ? new SimpleConnectionHandle(con) : null);
}

Calling the connections's setReadOnly method would fix the issue and enable the MySQL replication driver to address the correct DBMS:

...
    Connection con = session.getAccessor().getConnection();
    con.setReadOnly(readOnly);
    return (con != null ? new SimpleConnectionHandle(con) : null);
}

Could this be a Spring bug or are we missing anything in our application configuration?
Is there any reason why the current getJdbcConnection implementation ignores the present "readOnly" argument?


Affects: 3.0.5

Issue Links:

7 votes, 8 watchers

Activity

  1. spring-projects-issues commented on Feb 4, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Florian Feigenbutz commented

    Oh, I forgot to set a component when posting the issue and now I'm no more allowed to modify it.

    Guess the correct component would be "SpringCORE" or maybe "SpringTX"?

  2. spring-projects-issues commented on Feb 4, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Drotbohm commented

    Assigned to data access and transaction component.

  3. spring-projects-issues commented on Aug 1, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Florian Feigenbutz commented

    Any ideas for this issue?

  4. spring-projects-issues commented on Aug 2, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    The problem here is that getJdbcConnection is being called for exposure of a JDBC Connection to JDBC-based access code (e.g. when using JdbcTemplate within a JpaTransactionManager transaction), not for initializing the underlying Connection at the beginning of a transaction. Calling setReadOnly wouldn't work reliably there since it'd be called too late (in the middle of a transaction), or even not at all.

    The corresponding JpaDialect method for initializing a transaction is beginTransaction which also receives the readOnly flag through its TransactionDefinition argument. Unfortunately I am not aware of EclipseLink API which would allow us to set a readOnly flag for exposure on the JDBC Connection there. We can control early assignment of a Connection but don't get a callback for initializing that Connection...

    Juergen

  5. spring-projects-issues commented on Aug 4, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Florian Feigenbutz commented

    Okay, I see the reason for not only adapting getJdbcConnection().

    On the other hand it seems to work in my application.
    I'm using a LocalContainerEntityManagerFactoryBean which configures EclipseLink to use its internal JDBC connection pool by providing the database credentials using setJpaProperties instead of injecting a DataSource:

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaProperties">
    <props>
    ...
    <prop key="javax.persistence.jdbc.driver">...</prop>
    <prop key="javax.persistence.jdbc.url">...</prop>
    <prop key="javax.persistence.jdbc.user">...</prop>
    <prop key="javax.persistence.jdbc.password">...</prop>
    ...
    <prop key="eclipselink.jdbc.read-connections.initial">...</prop>
    <prop key="eclipselink.jdbc.read-connections.min">...</prop>
    ...
    </props>
    </property>
    ...

    <!--
    SETTING THE DATASOURCE INSTRUCTS ECLIPSELINK TO DISABLE ITS INTERNAL
    CONNECTION POOLS BECAUSE IT EXPECTS THE EXTERNAL DATASOURCE TO BE POOLED
    <property name="dataSource" ref="dataSource"/>
    -->
    </bean>

    Could this setup be the reason why it works in my application?

    Or could it be related to the following comment in org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect.beginTransaction():
    ...
    // This is the magic bit. As with the existing Spring TopLink integration,
    // begin an early transaction to force EclipseLink to get a JDBC Connection
    // so that Spring can manage transactions with JDBC as well as EclipseLink.
    ...
    Does this mean that EclipseLink will always use JDBC connections provided by the Spring Transaction Manager/JPA dialect? Or will use its own JDBC Connections anyway?

    Retrieving a readonly session from EclipseLink could be possible using org.eclipse.persistence.internal.jpa.EntityManagerImpl.getReadOnlySession() but I'm not totally sure if this is what you were looking for?

    Florian

  6. spring-projects-issues commented on Oct 28, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Due to EclipseLink's architecture, applying the read-only setting to an underlying JDBC Connection is quite hard: In particular after the fix for #12409, EclipseLink may not fetch a JDBC Connection at all for read operations which can be satisfied through the shared cache. As a consequence, enforcing a JDBC Connection to call setReadOnly on it may have a net negative effect: It may bypass the shared cache to begin with, unnecessarily going to the database (to a slave then but still unnecessarily).

    I'd rather advise to use Spring's IsolationLevelDataSourceAdapter for your DataSource setup, which not only applies a transaction-specific isolation level but also a transaction-specific read-only setting to the JDBC Connection. If your EntityManagerFactory setup points to that DataSource adapter, you'll implicitly get the read-only setting applied whenever EclipseLink decides to fetch a JDBC Connection.

    If there is anything else we can do here to improve the story for such a scenario, let me know. I'd also be interested in EclipseLink-specific improvements for read-only transactions at the Session level, i.e. to suppress unnecessary flushes etc.

    Juergen

  7. added
    status: ideal-for-contribution
    in: data
    type: enhancement
    on Jan 11, 2019
  8. added
    status: bulk-closed
    and removed
    status: ideal-for-contribution
    type: enhancement
    on Jan 11, 2019
  9. spring-projects-issues commented on Jan 12, 2019

    @spring-projects-issues
    CollaboratorAuthor

    Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: datastatus: bulk-closed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions