Eclipselink Pooling相当于C3PO [英] Eclipselink Pooling equivalent to C3PO

查看:161
本文介绍了Eclipselink Pooling相当于C3PO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阻止此日志记录

I am trying to prevent this logging


从服务器成功收到的最后一个数据包是10,255
毫秒之前。成功发送到服务器的最后一个数据包是
0毫秒前。

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.

我已经设置了自动重新连接的连接URL在 persistence.xml

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

我想要的是会有一个连接池,每分钟或每小时检查连接,以便连接是还活着。 Hibernate 具有c3po的此功能。喜欢ff。

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>

无论如何我可以在eclipselink中执行此操作吗?

is there anyway that I could do this in eclipselink?

推荐答案

更新2014年11月18日

我找到了我原先提供的答案有一些问题!检索到的密码由eclipselink加密,因此我们无法直接使用它。我们可以在这里硬编码我们的密码,但这可能不太好。我发现更好的方法是在创建实体管理器工厂时传递自定义DataSource对象。

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);

请在此处查看示例代码: https://github.com/jiakuan/wise-persist/blob/master/src/main /java/org/wisepersist/EntityManagerFactoryProvider.java

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

原始答案:

我遇到了同样的问题,我只是想通过创建自定义SessionCustomizer来使用eclipselink来使用bonecp数据源(c3p0应该类似)。这样的事情:

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;
  }
}

如果你想将c3p0与eclipselink一起使用,也许你只需要使用本页提到的代码( http://www.mchange.com/projects / c3p0 / #using_combopooleddatasource )在buildDataSource方法中。

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.

一些有用的链接:

  • 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/

这篇关于Eclipselink Pooling相当于C3PO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆