Eclipselink Pooling equivalent to C3PO

空扰寡人 提交于 2019-12-08 08:25:00

问题


I am trying to prevent this logging

The last packet successfully received from the server was 10,255 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.

I already set the the connection url with auto reconnect in the persistence.xml

What I want is that there will be a connection pool, check the connection every minute or hour so the connection is still alive. The Hibernate has this feature with c3po. like the ff.

  <property name="hibernate.c3p0.timeout">1800</property> <!-- seconds -->
  <property name="hibernate.c3p0.min_size">5</property> 
  <property name="hibernate.c3p0.max_size">50</property>    
  <property name="hibernate.c3p0.max_statements">50</property>
  <property name="hibernate.c3p0.timeout">50</property>

  <property name="hibernate.c3p0.numHelperThreads">5</property>
  <property name="hibernate.c3p0.maxAdministrativeTaskTime">5</property>
  <property name="statementCacheNumDeferredCloseThreads">1</property>

  <property name="hibernate.c3p0.validate">true</property>
  <property name="hibernate.c3p0.preferredTestQuery">select 1;</property>
  <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>

  <property name="hibernate.c3p0.automaticTestTable">C3P0</property>

is there anyway that I could do this in eclipselink?


回答1:


Update Nov 18, 2014:

I found the answer I provided originally has some issues! The password retrieved is encrypted by eclipselink so we couldn't use it directly. We can hard-code our password here, but that may not be nice. A better way I found was to pass a custom DataSource object when creating the entity manager factory.

additionalProperties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, dataSource);
emf = Persistence.createEntityManagerFactory(persistUnit, additionalProperties);

Please see the sample code here: https://github.com/jiakuan/wise-persist/blob/master/src/main/java/org/wisepersist/EntityManagerFactoryProvider.java

Original Answer:

I'm having the same issue, and I just figured out that I could use bonecp datasource (c3p0 should be similar) with eclipselink by creating a custom SessionCustomizer. Something like this:

public class JpaSessionCustomizer implements SessionCustomizer {

  private static final Logger log = LoggerFactory.getLogger(JpaSessionCustomizer.class);

  @Override
  public void customize(Session session) throws Exception {
    DatabaseLogin databaseLogin = session.getLogin();

    String jdbcDriver = databaseLogin.getDriverClassName();
    String jdbcUrl = databaseLogin.getDatabaseURL();
    String username = databaseLogin.getUserName();
    // WARNING: databaseLogin.getPassword() is encrypted,
    // which cannot be used directly here
    String password = "please use hard-coded password here";
    log.debug("jdbcDriver={}, jdbcUrl={}, username={}, password={}",
              jdbcDriver, jdbcUrl, username, password);

    BoneCPDataSource dataSource = buildDataSource(jdbcDriver, jdbcUrl, username, password);
    databaseLogin.setConnector(new JNDIConnector(dataSource));
  }

  private BoneCPDataSource buildDataSource(String jdbcDriver,
                                           String jdbcUrl,
                                           String username,
                                           String password) {
    BoneCPDataSource dataSource = new BoneCPDataSource();
    dataSource.setDriverClass(jdbcDriver); // Loads the JDBC driver
    dataSource.setJdbcUrl(jdbcUrl);
    dataSource.setUsername(username);
    dataSource.setPassword(password);

    dataSource.setConnectionTimeout(15, TimeUnit.SECONDS);
    dataSource.setAcquireRetryAttempts(10);

    dataSource.setConnectionTestStatement("SELECT 1");
    dataSource.setIdleConnectionTestPeriodInSeconds(30);

    dataSource.setPartitionCount(2);
    dataSource.setMinConnectionsPerPartition(5);
    dataSource.setMaxConnectionsPerPartition(10);

    dataSource.setDisableConnectionTracking(true);
    return dataSource;
  }
}

If you want to use c3p0 with eclipselink, perhaps you just need to use the code mentioned in this page (http://www.mchange.com/projects/c3p0/#using_combopooleddatasource) in the buildDataSource method.

Some useful links:

  • How to create a custom SessionCustomizer: http://wiki.eclipse.org/Customizing_the_EclipseLink_Application_(ELUG)#Using_the_Session_Customizer_Class
  • A suggestion on how to configure Eclipselink to use c3p0: http://www.eclipse.org/forums/index.php/t/172073/



回答2:


with respect to: WARNING: databaseLogin.getPassword() is encrypted;

You can use the following:

Map<Object, Object> props = session.getProperties();
...
dataSource.setPassword((String) props.get("javax.persistence.jdbc.password"));
...

regards!!



来源:https://stackoverflow.com/questions/17828377/eclipselink-pooling-equivalent-to-c3po

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!