I'm using JPA for my persistence for my project and don't really know anything about hibernate but most tutorials I follow have found to setup connection pooling use c3p0 and hibernate.
Heres my persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
<persistence-unit name="Cinemango308PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>JPA.Ad</class>
<class>JPA.Photo</class>
<class>JPA.Theatre</class>
<class>JPA.Creditcard</class>
<class>JPA.Moviereview</class>
<class>JPA.Giftcard</class>
<class>JPA.Showtime</class>
<class>JPA.Ticket</class>
<class>JPA.Favoritetheatres</class>
<class>JPA.User</class>
<class>JPA.Actor</class>
<class>JPA.Movie</class>
<class>JPA.Theatrerewards</class>
<class>JPA.Payment</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:5432/cinemango>
<property name="javax.persistence.jdbc.user" value="xxxx"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="xxxxxx"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<!-- Configuring Connection Pool -->
<property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="5" />
<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>
How do i test if the connection pooling is working? I don't see a log displayed to my output
Please find the logging configuration here http://www.mchange.com/projects/c3p0/#configuring_logging
There you can find how to switch on debug to see if pooling happens.
Related
i've tried to put my persistence.xml in various parts of my Java project and i call it with this command:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("my-unit");
but it returns me an error:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named my-unit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at main.MainClass.main(MainClass.java:81)
Process finished with exit code 1
my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
<persistence-unit name="my-unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.CharSet" value="utf8"/>
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- use encache provider-->
<!--<property name="hibernate.cache.region.factory_class"-->
<!--value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>-->
<!--<property name="hibernate.javax.cache.provider" value="org.ehcache.jsr107.EhcacheCachingProvider"/>-->
<!--<!–use second level cache–>-->
<!--<property name="hibernate.cache.use_second_level_cache" value="true"/>-->
<!--<!–use 3rd level cache–>-->
<!--<property name="hibernate.cache.use_query_cache" value="true"/>-->
</properties>
</persistence-unit>
I use IntelliJ as IDE for Java and the persistence.xml file is under META-INF folder in resources folder, i have tried to check the project structure and all looks like nice.
I use hibernate as maven repository.
Thanks for helping
You need to define the persistence provider name
<provider>org.hibernate.ejb.HibernatePersistence</provider>
That will solve the problem.
The example is from here
jpa\example\src\test\resources\META-INF\persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="jpa.sample.plain">
<class>example.springdata.jpa.simple.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
I still see the issue after versions in jpa\pom.xml were updated and look like:
<properties>
<hibernate.version>5.2.10.Final</hibernate.version>
<spring.version>4.3.8.RELEASE</spring.version>
</properties>
I am using hibernate 4.2 and working on J2ee6 with tomee 1.7.4 . I need to write multi-tenant code, which can connect to various databases on demand. I tried doing this by creating multiple persistence units in persistence.xml, but during server startup tomee tries to validate connection to all persistence units(all of them might not be available during testing).
I tried to find some setting that tells tomee to skip validation of the connections at startup, but couldn't find one. So instead of creating entitymanager from persistence unit, I started using the function
javax.persistence.Persistence.createEntityManagerFactory(String persistenceUnitName, Map properties)
and my persistence xml did not had any properties. This helped me solve this problem, but my caching stopped working when i moved to this model, it was working previously.
Can any one suggest some way in which i can ask tomee to skip validating persistence units at startup, or i can enable caching in the other way that i found.
My previous persistence.xml looked like
<?xml version="1.0" encoding="UTF-8" ?><persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="localDB" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/xxxxxx?autoReconnect=true" />
<property name="hibernate.connection.username" value="xxxxx" />
<property name="hibernate.connection.password" value="xxxxx" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.c3p0.acquire_increment" value="1"/>
<property name="hibernate.c3p0.max_size" value="40"/>
<!-- it must be set to LESS than the wait_timout setting for the mysql server (this setting defaults to 28800 secs (8 hours)) -->
<property name="hibernate.c3p0.idle_test_period" value="28680" />
<property name="hibernate.c3p0.preferredTestQuery" value="select 1;" />
<property name="hibernate.c3p0.timeout" value="60000"/>
<property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
<property name="hibernate.c3p0.debugUnreturnedConnectionStackTraces " value="true"/>
<property name="debugUnreturnedConnectionStackTraces " value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.region.factory_class" value="com.mc.hibernate.memcached.MemcachedRegionFactory" />
<property name="hibernate.memcached.operationTimeout" value = "40000"/>
<property name="hibernate.memcached.connectionFactory" value = "KetamaConnectionFactory"/>
<property name="hibernate.memcached.hashAlgorithm" value = "HashAlgorithm.FNV1_64_HASH"/>
<property name="hibernate.memcached.servers" value = "xxxxx:xxxx"/>
<property name="hibernate.cache.region_prefix" value=""/>
</properties>
</persistence-unit>
<persistence-unit name="production" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://xxxxx:3306/thewalkindb?autoReconnect=true" />
<property name="hibernate.connection.username" value="xxxx" />
<property name="hibernate.connection.password" value="xxxxx" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.c3p0.acquire_increment" value="1"/>
<property name="hibernate.c3p0.max_size" value="15"/>
<!-- it must be set to LESS than the wait_timout setting for the mysql server (this setting defaults to 28800 secs (8 hours)) -->
<property name="hibernate.c3p0.idle_test_period" value="28680" />
<property name="hibernate.c3p0.preferredTestQuery" value="select 1;" />
<property name="hibernate.c3p0.timeout" value="60000"/>
<property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
<property name="hibernate.c3p0.debugUnreturnedConnectionStackTraces " value="true"/>
<property name="debugUnreturnedConnectionStackTraces " value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.region.factory_class" value="com.mc.hibernate.memcached.MemcachedRegionFactory" />
<property name="hibernate.memcached.operationTimeout" value = "40000"/>
<property name="hibernate.memcached.connectionFactory" value = "KetamaConnectionFactory"/>
<property name="hibernate.memcached.hashAlgorithm" value = "HashAlgorithm.FNV1_64_HASH"/>
<property name="hibernate.memcached.servers" value = "xxxxxxxx"/>
<property name="hibernate.cache.region_prefix" value=""/>
</properties>
</persistence-unit>
this works with caching , but it doesnot gives me flexibility to skip validation at tomee startup
My new persistence xml which does not honours caching, but allows me the flexibility is as follows
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="localDB" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties></properties>
</persistence-unit>
<persistence-unit name="production" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
</properties>
</persistence-unit>
I am populating all the properties in a map and calling the function
javax.persistence.Persistence.createEntityManagerFactory(String persistenceUnitName, Map properties), but somehow it does not honours caching.
I don't get the link between validation and caching. If you do a facade to the entity manager tenant aware you just have caching per entity manager which means you will not check local cache for production persistence unit.
If you want a single entity manager - =you'll get a single JPA cache in this mode which will merge local and production - you can use dynamic routing: http://tomee.apache.org/examples-trunk/dynamic-datasource-routing/README.html instead of dynamic persistence units.
I've setted up Hibernate on Glassfish 4.1 but I'm having problems with persistence.
I'm able to read data, but cannot write to BD (changes appear to not be commited).
My current persistent.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myDataSource</jta-data-source>
<properties>
<property name="transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
</properties>
</persistence-unit>
</persistence>
My connection pool config on Glassfish is:
<jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" steady-pool-size="2" name="myPool" res-type="javax.sql.DataSource">
<property name="TrustServerCertificate" value="false"></property>
<property name="User" value="sa"></property>
<property name="LastUpdateCount" value="true"></property>
<property name="ResponseBuffering" value="adaptive"></property>
<property name="URL" value="jdbc:sqlserver://server\bd"></property>
<property name="XopenStates" value="false"></property>
<property name="PacketSize" value="8000"></property>
<property name="Password" value="mypass"></property>
<property name="ApplicationName" value="Microsoft JDBC Driver for SQL Server"></property>
<property name="DatabaseName" value="MyDB"></property>
<property name="Encrypt" value="false"></property>
<property name="LockTimeout" value="-1"></property>
<property name="SendStringParametersAsUnicode" value="true"></property>
<property name="MultiSubnetFailover" value="false"></property>
<property name="ApplicationIntent" value="readwrite"></property>
<property name="LoginTimeout" value="15"></property>
<property name="WorkstationID" value="My-MacBook-Pro.local"></property>
<property name="ServerName" value="xpto"></property>
<property name="PortNumber" value="1433"></property>
<property name="SelectMethod" value="direct"></property>
<property name="SendTimeAsDatetime" value="true"></property>
</jdbc-connection-pool>
Datasource config:
<jdbc-resource pool-name="myPool" jndi-name="jdbc/myDataSource"></jdbc-resource>
My EJB looks like this:
#PersistenceContext
private EntityManager em;
public void updateUser(User u) {
em.merge(u);
}
Any idea how I can fix that?
Thanks!
In my case, I was running the Hibernate 5 with tomcat and stop working when I changed to glassfish 4.1
The reason was the oldest jboss-logging.jar at: "YOUR_GLASSFISH_FOLDER/glassfish/modules"
Why? The hibernate 5 has dependency with the newest version of jboss-logging, and the glassfish uses the oldest version even if you declare inside your POM file the newest version. Actually I'm using:
org.jboss.logging jboss-logging 3.3.0.Final
Then I downloaded and replace the old .jar inside modules path and back to work, I spent 2 days trying to solve that and I hope it helps some future issues =D
I used this link to help me: https://medium.com/#mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz
Could you please try the following configuration:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="YOUR_PERSISTANCE_NAME" transaction-type="RESOURCE_LOCAL">
<provider>YOUR_PROVIDER</provider>
<!-- ENTITIES -->
<class>com.company.project....EntityA</class>
<class>com.company.project....EntityB</class>
<class>com.company.project....EntityC</class>
<properties>
<property name="javax.persistence.jdbc.url" value="YOUR_URL_TO_DB" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="USER" />
<property name="javax.persistence.jdbc.password" value="PASS" />
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.target-database" value="PostgreSQL" />
</properties>
</persistence-unit>
Running very simple example in a multi-threading java 7 application:
em.getTransaction().begin();
SimpleObject simpleObject = ...;
em.persist(simpleObject);
em.getTransaction().commit();
causes following error:
java.lang.IllegalStateException: Exception Description: Transaction is currently active
Running in a one thread all is fine.
My persistence.xml settings:
<persistence version="2.1"
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">
<persistence-unit name="mongo" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>test\SimpleObject</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform" />
<property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec" />
<property name="eclipselink.nosql.property.mongo.host" value="localhost" />
<property name="eclipselink.nosql.property.mongo.port" value="27017" />
<property name="eclipselink.nosql.property.mongo.db" value="test" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.nosql.property.mongo.write-concern" value="MAJORITY" />
<property name="eclipselink.jpa.uppercase-column-names" value="true" />
</properties>
</persistence-unit>
</persistence>
Version of used libraries:
EclipseLink 2.5.2,
mongoDB 2.6.1,
jpa 2.1.0,
mongo-java-driver-2.12.2,
eclipse.persistence.nosql-2.5.1
Is there anything wrong?
Thank you!