How to set a HikariCP DataSource as the JPA DataSource?

4,316 views
Skip to first unread message

Ryan Asleson

unread,
Mar 2, 2014, 7:39:43 PM3/2/14
to hika...@googlegroups.com

I'm sure a lot of this is because of my inexperience with JPA (I have a lot more experience with JDO) but the more I learn of JPA, the less I like it, mostly because of its over-reliance on the persistence.xml configuration and the inability to configure programmatically via Java.

Anyway, when I'm using JPA, how do I tell the EntityManagerFactory to use a HikariCP data source, rather than specifying a JNDI data source?  I've searched and searched but all I find is how to configure persistence.xml to look up a JNDI data source by name.

I'm creating my own HikariCP data source objects and I simply want to tell the JPA EntityManagerFactory to use that data source.

How do I do that?  It shouldn't be this hard.  I must be missing something.

Thank you!!!

-Ryan


Brett Wooldridge

unread,
Mar 3, 2014, 2:32:06 AM3/3/14
to hika...@googlegroups.com
Does the wiki page for Spring+Hibernate help?

https://github.com/brettwooldridge/HikariCP/wiki/Spring-Hibernate

That recipe was provided by somebody else, but appears to allow setting the
DataSource into the EntityManager ... in the case of Spring via the
LocalContainerEntityManagerFactoryBean.

-Brett

Ryan Asleson

unread,
Mar 3, 2014, 7:12:34 AM3/3/14
to hika...@googlegroups.com
Thanks, I'll take a look.  Right now I'm building a proof of concept app that isn't using Spring so I was hoping to avoid another jar.  The "real" app would eventually use Spring in some fashion, I'm sure, but for now I was hoping to avoid it.

I'll take a look and keep looking around, too.

Ryan Asleson

unread,
Mar 3, 2014, 2:29:47 PM3/3/14
to hika...@googlegroups.com
OK, I was able to do it using Spring's LocalContainerEntityManagerFactoryBean object.  I think it's a real strike against JPA that one has to use an external framework consisting of six other jars to create an EntityManagerFactory using a given DataSource, but oh well.  

Thank you!

Brett Wooldridge

unread,
Mar 4, 2014, 7:00:15 AM3/4/14
to hika...@googlegroups.com
Everywhere I've searched for an answer has basically come up empty Re: setting up JPA in Tomcat without JNDI.  But I'm glad you
found a configuration that works.

-Brett

Ryan Asleson

unread,
Mar 4, 2014, 8:05:25 AM3/4/14
to hika...@googlegroups.com

For those who may stumble upon this later, here's what I did.  

Notes:

1.  dataSource is the HikariCP DataSource you created yourself.
2.  This assumes that you have a persistence.xml somewhere on the classpath, as you normally would.  Spring will create the EntityManagerFactory based on the settings in persistence.xml but will use the DataSource you provided.  I think you can set more properties here that will override what's in persistence.xml but I didn't do that myself.
3.  Be sure to call the afterPropertiesSet() method.  It's important!  That method actually triggers the creation of the EntityManagerFactory, otherwise calling getNativeEntityManagerFactory will return null.


      LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
      factoryBean.setDataSource(dataSource);
      factoryBean.setPersistenceUnitName("persistenceUnitName");
      factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
      factoryBean.afterPropertiesSet();

      factory = factoryBean.getNativeEntityManagerFactory();

Reply all
Reply to author
Forward
0 new messages