spring - Using hibernate.c3p0 when creating a EntityManagerFactoryBean in Java -
i moved .net world java world.
i have build core project used multiple clients (web client, api, ...)
doing got tired of having configure data access beans , connection strings in multiple projects. goal include "core-project.jar" , able use without having configure data access beans , connection strings.
i found this: (seems have trouble webserver atm.) http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/
which resulted in code works , completes goal.
baserepository
package com.company.core.data.repositories; import javax.persistence.entitymanagerfactory; import javax.persistence.persistenceunit; public abstract class baserepository { protected entitymanagerfactory emf; @persistenceunit public void setentitymanagerfactory(entitymanagerfactory emf) { this.emf = emf; } }
userrepository
package com.company.core.data.repositories; import com.company.core.data.interfaces.iuserrepository; import com.company.core.data.pojos.userpojo; import org.springframework.stereotype.component; import javax.persistence.entitymanager; import javax.persistence.entitytransaction; import java.util.list; @component public class userrepository extends baserepository implements iuserrepository { public userpojo getbyemail(string email) throws exception { entitymanager em = emf.createentitymanager(); list<userpojo> results = em.createquery("from userpojo u u.email = ?", userpojo.class) .setparameter(1, email) .getresultlist(); em.close(); return firstordefault(results); } public static <t> t firstordefault(list<t> list){ if(list.size() > 0) return list.get(0); return null; } }
userpojo
package com.company.core.data.pojos; import java.util.date; import javax.persistence.*; @entity @table( name = "user" ) public class userpojo { // properties }
entitymanagerfactoryconfiguration
package com.company.core.configuration; import java.util.properties; import javax.persistence.entitymanagerfactory; import javax.sql.datasource; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.dao.annotation.persistenceexceptiontranslationpostprocessor; import org.springframework.jdbc.datasource.drivermanagerdatasource; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; @configuration @enabletransactionmanagement @componentscan({"com.company.core.data"}) public class entitymanagerfactoryconfiguration { public entitymanagerfactoryconfiguration() { super(); } @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() { final localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean(); em.setdatasource(datasource()); em.setpackagestoscan(new string[]{"com.company.core.data.pojos"}); final hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter(); em.setjpavendoradapter(vendoradapter); properties hibernateproperties = new properties(); hibernateproperties.setproperty("hibernate.dialect", "org.hibernate.dialect.mysql5innodbdialect"); hibernateproperties.setproperty("hibernate.show_sql", "false"); // hibernateproperties.setproperty("hibernate.connection.provider_class", "org.hibernate.connection.c3p0connectionprovider"); // hibernateproperties.setproperty("hibernate.c3p0.max_size", "100"); // hibernateproperties.setproperty("hibernate.c3p0.min_size", "0"); // hibernateproperties.setproperty("hibernate.c3p0.acquire_increment", "1"); // hibernateproperties.setproperty("hibernate.c3p0.idle_test_period", "200"); // hibernateproperties.setproperty("hibernate.c3p0.max_statements", "0"); // hibernateproperties.setproperty("hibernate.c3p0.timeout", "300"); em.setjpaproperties(hibernateproperties); return em; } @bean public datasource datasource() { final org.springframework.jdbc.datasource.drivermanagerdatasource datasource = new org.springframework.jdbc.datasource.drivermanagerdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://127.0.0.1/databasename?autoreconnect=true"); datasource.setusername("user"); datasource.setpassword("password"); return datasource; } @bean public datasource c3p0datasource() { final com.mchange.v2.c3p0.drivermanagerdatasource datasource = new com.mchange.v2.c3p0.drivermanagerdatasource(); datasource.setdriverclass("com.mysql.jdbc.driver"); datasource.setjdbcurl("jdbc:mysql://127.0.0.1/databasename?autoreconnect=true"); datasource.setuser("user"); datasource.setpassword("password"); return datasource; } @bean public platformtransactionmanager transactionmanager(final entitymanagerfactory emf) { final jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(emf); return transactionmanager; } @bean public persistenceexceptiontranslationpostprocessor exceptiontranslation() { return new persistenceexceptiontranslationpostprocessor(); } }
as can want use hibernate.c3p0 connection management.
commenting in outcommented hibernateproperties results in:
javax.persistence.persistenceexception: org.hibernate.exception.genericjdbcexception: not open connection @ org.hibernate.jpa.spi.abstractentitymanagerimpl.convert(abstractentitymanagerimpl.java:1763) @ org.hibernate.jpa.spi.abstractentitymanagerimpl.convert(abstractentitymanagerimpl.java:1677) @ org.hibernate.jpa.spi.abstractentitymanagerimpl.throwpersistenceexception(abstractentitymanagerimpl.java:1771) @ org.hibernate.jpa.internal.transactionimpl.begin(transactionimpl.java:64) @ com.company.core.data.repositories.userrepository.getbyemail(userrepository.java:30) @ com.company.core.business.implementations.userservice.getbyemail(userservice.java:18) @ com.company.api.usercontroller.getbyemail(usercontroller.java:34) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:137) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:110) @ org.springframework.web.servlet.mvc.method.annotation.exceptionhandlerexceptionresolver.doresolvehandlermethodexception(exceptionhandlerexceptionresolver.java:362) @ org.springframework.web.servlet.handler.abstracthandlermethodexceptionresolver.doresolveexception(abstracthandlermethodexceptionresolver.java:60) @ org.springframework.web.servlet.handler.abstracthandlerexceptionresolver.resolveexception(abstracthandlerexceptionresolver.java:138) @ org.springframework.web.servlet.dispatcherservlet.processhandlerexception(dispatcherservlet.java:1183) @ org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1020) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:971) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:893) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:966) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:868) @ javax.servlet.http.httpservlet.service(httpservlet.java:648) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842) @ javax.servlet.http.httpservlet.service(httpservlet.java:729) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:291) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:186) @ org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:160) @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:344) @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:261) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:610) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:518) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1091) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:668) @ org.apache.coyote.http11.http11aprprotocol$http11connectionhandler.process(http11aprprotocol.java:283) @ org.apache.tomcat.util.net.aprendpoint$socketprocessor.dorun(aprendpoint.java:2463) @ org.apache.tomcat.util.net.aprendpoint$socketprocessor.run(aprendpoint.java:2452) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745) caused by: org.hibernate.exception.genericjdbcexception: not open connection @ org.hibernate.exception.internal.standardsqlexceptionconverter.convert(standardsqlexceptionconverter.java:54) @ org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:126) @ org.hibernate.engine.jdbc.spi.sqlexceptionhelper.convert(sqlexceptionhelper.java:112) @ org.hibernate.engine.jdbc.internal.logicalconnectionimpl.obtainconnection(logicalconnectionimpl.java:235) @ org.hibernate.engine.jdbc.internal.logicalconnectionimpl.getconnection(logicalconnectionimpl.java:171) @ org.hibernate.engine.transaction.internal.jdbc.jdbctransaction.dobegin(jdbctransaction.java:67) @ org.hibernate.engine.transaction.spi.abstracttransactionimpl.begin(abstracttransactionimpl.java:162) @ org.hibernate.internal.sessionimpl.begintransaction(sessionimpl.java:1435) @ org.hibernate.jpa.internal.transactionimpl.begin(transactionimpl.java:61) ... 50 more caused by: java.sql.sqlexception: connections not acquired underlying database! @ com.mchange.v2.sql.sqlutils.tosqlexception(sqlutils.java:118) @ com.mchange.v2.c3p0.impl.c3p0pooledconnectionpool.checkoutpooledconnection(c3p0pooledconnectionpool.java:689) @ com.mchange.v2.c3p0.impl.abstractpoolbackeddatasource.getconnection(abstractpoolbackeddatasource.java:140) @ org.hibernate.c3p0.internal.c3p0connectionprovider.getconnection(c3p0connectionprovider.java:90) @ org.hibernate.internal.abstractsessionimpl$noncontextualjdbcconnectionaccess.obtainconnection(abstractsessionimpl.java:380) @ org.hibernate.engine.jdbc.internal.logicalconnectionimpl.obtainconnection(logicalconnectionimpl.java:228) ... 55 more caused by: com.mchange.v2.resourcepool.cannotacquireresourceexception: resourcepool not acquire resource primary factory or source. @ com.mchange.v2.resourcepool.basicresourcepool.awaitavailable(basicresourcepool.java:1418) @ com.mchange.v2.resourcepool.basicresourcepool.prelimcheckoutresource(basicresourcepool.java:606) @ com.mchange.v2.resourcepool.basicresourcepool.checkoutresource(basicresourcepool.java:526) @ com.mchange.v2.c3p0.impl.c3p0pooledconnectionpool.checkoutandmarkconnectioninuse(c3p0pooledconnectionpool.java:755) @ com.mchange.v2.c3p0.impl.c3p0pooledconnectionpool.checkoutpooledconnection(c3p0pooledconnectionpool.java:682) ... 59 more
i have tried create c3p0 datesource (see method c3p0datasource()9 no luck either.
i think might in on head, guidance appreciated!!
the xml configuration:
application-context.xml
<beans:bean id="emf" class="org.springframework.orm.jpa.localentitymanagerfactorybean"> <beans:property name="persistenceunitname" value="datasource"/> </beans:bean>
persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1"> <persistence-unit name="datasource"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1/database?autoreconnect=true" /> <property name="javax.persistence.jdbc.user" value="user" /> <property name="javax.persistence.jdbc.password" value="password" /> <!-- important --> <property name="hibernate.dialect" value="org.hibernate.dialect.mysql5innodbdialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.connection.provider_class" value="org.hibernate.connection.c3p0connectionprovider" /> <property name="hibernate.c3p0.max_size" value="100" /> <property name="hibernate.c3p0.min_size" value="0" /> <property name="hibernate.c3p0.acquire_increment" value="1" /> <property name="hibernate.c3p0.idle_test_period" value="300" /> <property name="hibernate.c3p0.max_statements" value="0" /> <property name="hibernate.c3p0.timeout" value="100" /> </properties> </persistence-unit> </persistence>
Comments
Post a Comment