web application structure hierarcy problem - java

Below an illustration of my project hierarchy
(source: yfrog.com)
When i try to connect my file_name.java file to hibernate i'm getting these errors
Exception in thread "main" org.hibernate.HibernateException: Could not instantiate cache implementation
at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:214)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1218)
at net.pkg.dao.FirstExample.main(FirstExample.java:17)
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
at org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21)
at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:61)
the problem has something to do with my hibernate.cfg.xml... but I'm unable to resolve it. Any ideas?

I suspect that you are using either #Cacheable or #Cache on your entities without having the second level cache activated, hence the error message:
o.h.c.NoCachingEnabledException: Second-level cache is not enabled for usage ...
You need something like this in the hibernate.cfg.xml to use the second level cache (I'm using EHCache as cache provider here):
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
Whether you really need the second level cache is another story.

Related

How to get EntityManager(s) for mulitple persistence units

I am writing a JEE7 application that runs in WebSphere Liberty Profile 8.5.5. We are using JPA (which is implemented via Eclipselink in WLP).
I have multiple persistence units in the same 'persistence.xml' file. I also need to access two of those units in the same class.
I am getting a runtime error when I try to use the second EntityManager:
#PersistenceContext(unitName = "wwer-list")
private EntityManager entityManagerWwerList;
#PersistenceContext(unitName = "main-dashboard")
private EntityManager entityManagerMainDashboard;
E WTRN0062E: An illegal attempt to use multiple resources that have only one-phase capability has occurred within a global transaction.
How do I get rid of this error?
Also, all of the tables I am using are only needed for reading. So how can I specify that I only want read-only access to JPA?
This issue is prompting because one of your datasource configured as (single phase commit) using ConnectionPoolDataSource and other is configured with XADataSource.
If you want to continue with the same datasource configuration, you will have to update your Server configuration to "Acccept Heuristic Hazard".
In the admin console, click the EAR, select the check box "Accept heuristic hazard". Re-start the server.
This link to enable the Last Participant Support may also help.
http://www.ibm.com/support/knowledgecenter/SSAW57_7.0.0/com.ibm.websphere.nd.doc/info/ae/webui_pme/ui/ueac_laoextensionsettings.html
I can't tell for sure without your persistence.xml and server.xml configurations, but it looks like the <dataSource> elements backing your <persistence-unit> configurations are not XA capable.
By default, a <dataSource> should be a javax.sql.XADataSource (and therefore XA capable), however if you are using a JDBC driver that does not provide an XADataSource implementation, Liberty will pick a simpler DataSource implementation (i.e. javax.sql.ConnectionPoolDataSource or plain javax.sql.DataSource).
A global transaction is whenever you issue a UserTransaction.begin() and lasts until you issue a commit() or a rollback(). There are other ways that you can get into a global transaction too.
Since you want read-only access, converting your DataSources to XA would probably be overkill. Instead, try to eliminate the global transactions from the equation. If you can't eliminate the global transactions, you can specify XADataSource in your server.xml in the following way:
<dataSource type="javax.sql.XADataSource" ...>
<jdbcDriver .../>
<properties .../>
</dataSource>

The synchronisaton in my MySql database requires the restart of my Java application

I have an application that it is used by more than one user through network sharing the same MySQL database. So we need that when the one makes changes on the database the other with simple refresh can see the changes.
I am working with EclipseLink (having the cache config as default).
Actually, the second user (wanting to catch changes made by the first user) has to restart the application (created by Netbeans).
UPDATE:
Here is the configuration in my persistence.xml.
Probably there is some cache in your applicaltion that holds the previus values retrieved from DB - so the sql you are invoking is not going to DB, but returns results from local cache of your app. According to your persistance configuration, look at the documentation how to disable the cache - it is probably on the ORM level.
from the EclipseLink documentation, how to disable the cache:
By default EclipseLink enables a shared object cache to cache objects read from the database to avoid repeated database access. If the database is changed directly through JDBC, or by another application or server, the objects in the shared cache will be stale.
EclipseLink offers several mechanism to deal with stale data including:
Refreshing
Invalidation
Optimistic locking
Cache coordination
The shared cache can also be disabled. This can be done using the EclipseLink persistence unit property:
<property name="eclipselink.cache.shared.default" value="false"/>
Or the JPA 2.0 persistence unit element:
<shared-cache-mode>NONE</shared-cache-mode>
Or can be selectively enabled/disabled using the #Cache annotation:
#Entity
#Cache(isolation=ISOLATED)
public class Employee {
...
}
Or the JPA 2.0 #Cacheable annotation:
#Entity
#Cacheable(false)
public class Employee {
...
}
Do not disable the cache by setting the CacheType to None, this can cause object identity issues.

Configure Query Cache in Hibernate4

I'm trying enable query cache in our application. As per the documentation I have given
hibernate.cache.use_query_cache as true and givent setCacheable() in my queries. But I'm getting below exception.
org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
Should I specify cache region for query cache, I'm using Hibernate 4. Please help me in this regard
Use Following for hibernate 4.
I am using this with hibernate 4.3
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>

How to log into separate files per thread using EclipseLink

Scenario: Multi-threaded application that interacts with a relational database using EclipseLink JPA Implementation
Desired: Having each thread to log SQL activity to a different file
The documentation states that it's possible to use the following property to specify which file the persistence unit should log to:
<property name="eclipselink.logging.file" value="output.log"/>
Also, the documentation states that it's possible to include a thread identifier into the set of logged informations using the following property:
<property name="eclipselink.logging.thread" value="true"/>
Unfortunately, the most one can get from the combination of these two properties is a unique file with logging information of all the threads in mixed order. This will likely force to post-process the file in some way (e.g. grep) to obtain a "log-per-thread".
Is there any way that I'm missing to obtain this? And if so, is it necessarily a programmatic solution?

Hibernate database creation

I'm trying to create database schema on application server startup.
I use hebernate 4.1.9 with annotations and hibernate.cfg.xml for configuration.
So the problem is that I cann't fully understand what I should do to create schema and after that use it in application. Of course I want to perform schema creation only on first start and on the next start I want to update it.
I'm trying to use hbn2ddl.auto in update state, but database doesn't creates. May be I should use somethin like INIT=create schema IF NOT EXISTS myschema in the end of the hibernate.connection.url??
Also I have an exception
org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
But in hibernate.cfg.xml there is such string:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
Can anybody describe it to me?
I solve the problem by myself. I should add createDatabase=true to hibernate.connection.url

Categories