Problem beim initialisieren des Caches

Dieses Thema Problem beim initialisieren des Caches im Forum "Data Tier" wurde erstellt von Nemesys, 3. Sep. 2014.

Thema: Problem beim initialisieren des Caches Hey zusammen, ich habe seit langem wieder ein Java-Projekt gestartet und wollte nun Hibernate integrieren. Das...

  1. Hey zusammen,

    ich habe seit langem wieder ein Java-Projekt gestartet und wollte nun Hibernate integrieren. Das hat soweit geklappt. Nun wollte ich aber das Caching testen. Sobald ich den Test ausführe erhalte ich folgenden Fehler:

    Code (Text):
    Initial SessionFactory creation failed.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
    Exception in thread "main" java.lang.ExceptionInInitializerError
        at hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
        at hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
        at TestHibernateEhcache.main(TestHibernateEhcache.java:12)
    Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:187)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:151)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:264)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1861)
        at hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
        ... 2 more
    Caused by: org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
        at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:110)
        at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:70)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:40)
        at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:35)
        at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:91)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:177)
        ... 8 more
    Caused by: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
        at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:103)
        at org.hibernate.cache.ehcache.internal.util.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:63)
        at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:93)
        ... 13 more
    Caused by: java.lang.NullPointerException
        at net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:100)
        ... 15 more
    Hier mein Test:

    Code (Text):
    import hibernate.HibernateUtil;
    import org.hibernate.Session;
    import org.junit.Test;
    import user_management.Accounts;


    public class TestHibernateEhcache {  
       
        @Test
        public static void main(String[] args){
            //Open the hibernate session
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Session sessionTemp = HibernateUtil.getSessionFactory().openSession();
            sessionTemp.beginTransaction();
            try
            {
                //fetch the department entity from database first time
                Accounts acc = (Accounts) session.load(Accounts.class, new Integer(1));
                System.out.println(acc.getName());

                //fetch the department entity again
                acc = (Accounts) session.load(Accounts.class, new Integer(1));
                System.out.println(acc.getName());

                acc = (Accounts) sessionTemp.load(Accounts.class, new Integer(1));
                System.out.println(acc.getName());
            }
            finally
            {
                session.getTransaction().commit();
                HibernateUtil.shutdown();

                sessionTemp.getTransaction().commit();
                HibernateUtil.shutdown();
            }
        }
    }
     
    Hier die Einstellungen von Hibernate:

    Code (Text):

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
        <!--  Database connection settings  -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/portal2?zeroDateTimeBehavior=convertToNull</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!--  JDBC connection pool (use the built-in)  -->
        <property name="connection.pool_size">1</property>
        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Cache Configurations -->
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
        <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>
        <property name="hibernate.cache.use_query_cache">true</property>
        <!--  Echo all executed SQL to stdout  -->
        <property name="show_sql">true</property>
        <!--  Drop and re-create the database schema on startup  -->
        <property name="hbm2ddl.auto">create</property>
        <!-- mapping entities -->
        <mapping class="user_management.Accounts"/>
      </session-factory>
    </hibernate-configuration>

     
    Was habe ich übersehen?

    Beim Objekt Accounts habe ich übrigens keine xml-Datei fürs Mapping erstellen lassen. Das läuft alles über Annotationen. Was ist hier das "Best Practice"?

    VG Matthias
     
  2. Vielleicht helfen dir diese Java-Grundlagen weiter --> *Klick*