javax.ejb.EJBTransactionRolledbackException - get stacktrace for origin - java

In my web application I use OpenJPA. Sometimes when I make database operation I get this exception:
javax.servlet.ServletException: javax.el.ELException: javax.ejb.EJBTransactionRolledbackException: Transaction was rolled back, presumably because setRollbackOnly was called during a synchronization
with root case:
org.apache.geronimo.transaction.manager.SetRollbackOnlyException: setRollbackOnly() called. See stacktrace for origin
The question is: how to get stacktrace for exception which was thrown in transaction?
I look here
https://issues.apache.org/jira/browse/GERONIMO-4576
https://issues.apache.org/jira/browse/OPENEJB-782
https://issues.apache.org/jira/browse/OPENEJB-980
but I don't understand it.
I think I get this exception every time when I try to save big file (more than 2MB) to the table at database.

I recommend to set this properties inside your persistece.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="laudoweb_pu">
<jta-data-source>jdbc/integracaolaudoweb</jta-data-source>
<class>com.xxx.xxxEntity</class>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="oracle" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.AutoDetach" value="close" />
<property name="openjpa.DetachState" value="loaded" />
<property name="openjpa.DataCache" value="false" />
<property name="openjpa.Optimistic" value="true" />
<property name="openjpa.Multithreaded" value="true" />
<property name="openjpa.TransactionMode" value="managed" />
<property name="openjpa.ConnectionFactoryMode" value="managed" />
<property name="openjpa.NontransactionalRead" value="true" />
<property name="openjpa.RestoreState" value="all" />
<property name="openjpa.ManagedRuntime" value="auto" />
</properties>
</persistence-unit>
</persistence>
Here the openjpa documentation for logs and other properties.
http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_logging_openjpa.html
Mainly this line is important for log and trace:
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>

Related

Configure JBOSS 7.1 to use application-managed entity manager as opposed to container-managed entity manager

I am trying to upgrade my application deployment from Jboss 6.4 to Jboss 7.1 and I've not been able to get past this error:
java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'entityManagerFactory' in deployment ***.war for injection point private javax.persistence.EntityManager com.***.persistenceManager"}}}}}}}}
I have entityManagerFactory defined as a bean in Spring's applicationContext.xml file as shown below:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="extiPU" />
<property name="dataSource" ref="dataSource" />
<property name="jtaDataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<!--<property name="database" value="ORACLE" />-->
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" />
<property name="generateDdl" value="true" />
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
</property>
</bean>
and the content of my persistence.xml is:
<?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="extiPU"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/tdsrc</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="OFF" />
<!--
<property name="eclipselink.deploy-on-startup" value="true" />
<property name="eclipselink.target-server" value="JBoss"/>
-->
<property name="eclipselink.logging.level.sql" value="OFF" />
<property name="eclipselink.logging.parameters" value="false" />
<property name="eclipselink.logging.connection" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.target-database" value="Oracle" />
<property name="eclipselink.weaving" value="false" />
<property name="jboss.as.jpa.managed" value="false" />
</properties>
</persistence-unit>
In my persistence classes, I inject it by doing:
#PersistenceContext(unitName = "entityManagerFactory")
private EntityManager persistenceManager;
With all these done, I still cannot figure out why Jboss 7.1 does not allow the persistence manager to be application managed.
Thanks

Hibernate Setup on Webservice Intellij

I am trying to setup a REST API using Hibernate as my ORM Mapper. As IDE I am using IntelliJ. The project folder structure looks like this:
projectstructure
I setup the project with Glassfish like this instructions
The REST-API part works fine so I won't bother pasting all of that in here. To test the Hibernate functionality I wrote this class:
public class ArtistRepository {
public static void testHibernate(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("emf");
EntityManager manager = factory.createEntityManager();
Artist green = new Artist();
green.setName("TEST");
manager.getTransaction().begin();
manager.persist(green);
manager.getTransaction().commit();
}
}
The persistence.xml looks like this:
<persistence 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_1_0.xsd"
version="1.0">
<persistence-unit name="emf">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/database?autoReconnect=true" />
<property name="hibernate.connection.username" value="*****" />
<property name="hibernate.connection.password" value="*****" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</properties>
</persistence-unit>
</persistence>
When I request a REST resource using that method I get the following error:
<p>
<b>exception</b>
<pre>javax.servlet.ServletException: javax.persistence.PersistenceException: No Persistence provider for EntityManager named emf</pre>
</p>
<p>
<b>root cause</b>
<pre>javax.persistence.PersistenceException: No Persistence provider for EntityManager named emf</pre>
</p>
<p>
<b>note</b>
<u>The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 5.0 logs.</u>
</p>
So obviousley the persistence.xml is in the wrong directory, but I have no clue where to put it.

JPA does not honours caching while creating entity manager factory dynamically

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.

Properly Refresh data from Database JPA - Hibernate

I'm currently using the JPA specs to query my objects from the database.
Everytime there's a change made by me (my instance of the software), the items would be properly refreshed.
But, if there's another change made by someone else (other instance, or database change), the items are'nt being properly refreshed.
I'm using a simple "find" with "refresh"
Object found = getManager().find(getModelClass(), id);
getManager().refresh(found);
I'm using a DAO Hierarchy, the "getModelClass" returns my #Entity class like
#Override
protected Class<?> getModelClass() {
return ProductCategory.class;
}
And my Manifest / persistence.xml:
<?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="casa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!-- localhost -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/database" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.use_sql_comments" value="false" />
<property name="hibernate.jdbc.wrap_result_sets" value="false" />
<property name="hibernate.hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Also each "DAO" have and instance of and EntityManager of it's own.
What I might be doing wrong ?
Appreciate the help!
Seems like my problem was simple.
The refresh transaction to be properly refreshed from the database needs to be commited
getManager().getTransaction().begin();
T found = (T) getManager().find(getModelClass(), id);
getManager().refresh(found);
getManager().getTransaction().commit();

OpenJPA Configuration in WAS 7.0.0.25

I am looking at the performance optimization of Open JPA for my WAS 7.0.0.25 server. I enabled OpenJPA trace from WAS console to understand what properties are taken for caching.
There is no openjpa.= type entry in my persistance.xml. But I can see entries in the trace.log as below:
openjpa.IgnoreChanges: false
openjpa.FlushBeforeQueries: 0
openjpa.ConnectionRetainMode: 0
But i could not find the configuration that sets these properties so that I can play around with them.
Can anyone guide me where I can find these values coming from the Open JPA provided by WAS 7?
Accordingly to the documentation you should add this line to your persistence.xml:
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_logging_openjpa.html
ADDED
If you add those keys in your persistence.xml, probably the default values will be overridden.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="...">
<properties>
<property name="openjpa.jdbc.DBDictionary" value="oracle" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
<property name="openjpa.AutoDetach" value="close" />
<property name="openjpa.DetachState" value="loaded" />
<property name="openjpa.DataCache" value="false" />
<property name="openjpa.Optimistic" value="true" />
<property name="openjpa.Multithreaded" value="true" />
<property name="openjpa.TransactionMode" value="managed" />
<property name="openjpa.ConnectionFactoryMode" value="managed" />
<property name="openjpa.NontransactionalRead" value="true" />
<property name="openjpa.RestoreState" value="all" />
<property name="openjpa.ManagedRuntime" value="auto" />
</properties>
</persistence-unit>
</persistence>
Xhings -
If you are looking for default configuration values, you should start by looking at the user manual. That beings said, the only property that I would recommend (from a performance point of view) to change is openjpa.ConnectionRetainMode. Setting that property to always has shown some performance impact.

Categories