20856

Eclipselink Pooling equivalent to C3PO

Question:

I am trying to prevent this logging

<blockquote>

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.

</blockquote>

I already set the the connection url with auto reconnect in the <strong>persistence.xml</strong>

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 <strong>Hibernate</strong> 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?

Answer1:

<strong>Update Nov 18, 2014</strong>:

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: <a href="https://github.com/jiakuan/wise-persist/blob/master/src/main/java/org/wisepersist/EntityManagerFactoryProvider.java" rel="nofollow">https://github.com/jiakuan/wise-persist/blob/master/src/main/java/org/wisepersist/EntityManagerFactoryProvider.java</a>

<strong>Original Answer:</strong>

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 (<a href="http://www.mchange.com/projects/c3p0/#using_combopooleddatasource" rel="nofollow">http://www.mchange.com/projects/c3p0/#using_combopooleddatasource</a>) in the buildDataSource method.

Some useful links:

<ul><li>How to create a custom SessionCustomizer: <a href="http://wiki.eclipse.org/Customizing_the_EclipseLink_Application_(ELUG)#Using_the_Session_Customizer_Class" rel="nofollow">http://wiki.eclipse.org/Customizing_the_EclipseLink_Application_(ELUG)#Using_the_Session_Customizer_Class</a></li> <li>A suggestion on how to configure Eclipselink to use c3p0: <a href="http://www.eclipse.org/forums/index.php/t/172073/" rel="nofollow">http://www.eclipse.org/forums/index.php/t/172073/</a></li> </ul>

Answer2:

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!!

Recommend

  • the answer is always wrong in this MIPS recursion . got 10, supposed to be 55
  • Use of Frame Pointer MIPS
  • MIPS Assembly - Arrays?
  • Why we cant compare (bne-beq) a register with an immediate (Assembly-MIPS)?
  • curl - How to escape < in parameter value
  • Why does Apple use assign rather than weak to store a delegate?
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • Symfony2 - Doctrine - no changeset in post update
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • Doctrine/Symfony entity generator and generating entity from one table
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Creating a DropDownList
  • Who propagate bugfixes across branches (corporate development)?
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • Force show.bind execution
  • Moving Android View and preventing onDraw to be called over and over again
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • C++ Partial template specialization - design simplification
  • Change Inet root folder for iis 7
  • Java: can you cast Class into a specific interface?
  • NHibernate Validation Localization with S#arp Architecture
  • Scrapy recursive link crawler
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Fetching methods from BroadcastReceiver to update UI
  • Bug in WPF DataGrid
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Redux, normalised entities and lodash merge
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?