diff --git a/pom.xml b/pom.xml index 2922f5c20b09e50fd17850442468033891c92467..9a6ed078ed292faff1ab84d4f36ae09d380f064e 100644 --- a/pom.xml +++ b/pom.xml @@ -269,26 +269,18 @@ mysql-connector-java 5.1.14 + org.hibernate - hibernate-entitymanager - 4.1.1.Final - - - javax.transaction - jta - 1.1 - - - org.hibernate.javax.persistence - hibernate-jpa-2.0-api - 1.0.1.Final + hibernate-core + 4.3.10.Final - + + diff --git a/src/META-INF/persistence.xml b/src/META-INF/persistence.xml deleted file mode 100644 index 64170b0bc5d336eb185111b57e7bff17b8d34e71..0000000000000000000000000000000000000000 --- a/src/META-INF/persistence.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/de/tud/kom/p2psim/impl/analyzer/metric/output/MetricOutputDAO.java b/src/de/tud/kom/p2psim/impl/analyzer/metric/output/MetricOutputDAO.java index 327e26fa3f3ac02ee871d65a1bb8d3b87781a5f1..fa67113c34f28255f7c3e4d13e01dc199f89b684 100644 --- a/src/de/tud/kom/p2psim/impl/analyzer/metric/output/MetricOutputDAO.java +++ b/src/de/tud/kom/p2psim/impl/analyzer/metric/output/MetricOutputDAO.java @@ -53,16 +53,15 @@ public class MetricOutputDAO extends AbstractOutput { */ @XMLConfigurableConstructor({ "table" }) public MetricOutputDAO(String table) { - DAO.configOverwrites.put("hibernate.connection.url", - "jdbc:mysql://localhost/" + table); + DAO.database = table; } public void setUser(String user) { - DAO.configOverwrites.put("hibernate.connection.username", user); + DAO.username = user; } public void setPassword(String password) { - DAO.configOverwrites.put("hibernate.connection.password", password); + DAO.password = password; } public void setTimeEnableDao(long timeEnableDao) { diff --git a/src/de/tud/kom/p2psim/impl/common/DefaultMonitor.java b/src/de/tud/kom/p2psim/impl/common/DefaultMonitor.java index dd6e93f8e135ff026f78b197934b6243da46a0a0..9160b132979023d924252c00dbce34c2d1069e92 100644 --- a/src/de/tud/kom/p2psim/impl/common/DefaultMonitor.java +++ b/src/de/tud/kom/p2psim/impl/common/DefaultMonitor.java @@ -141,8 +141,7 @@ public class DefaultMonitor implements MonitorComponent, EventHandler, public void setTableName(String tableName) { System.out.println("Table Name is set to: " + tableName); if (tableName != null && !tableName.equals("")) { - DAO.configOverwrites.put("hibernate.connection.url", - "jdbc:mysql://localhost/" + tableName); + DAO.database = tableName; } } diff --git a/src/de/tud/kom/p2psim/impl/simengine/Simulator.java b/src/de/tud/kom/p2psim/impl/simengine/Simulator.java index 53b3bf7919c02304151e108e31260a79f44a71ef..6f58274013cc0b2f79b35ab6d1a41d9cfbe561cd 100644 --- a/src/de/tud/kom/p2psim/impl/simengine/Simulator.java +++ b/src/de/tud/kom/p2psim/impl/simengine/Simulator.java @@ -475,8 +475,7 @@ public class Simulator implements RandomGeneratorComponent, GlobalComponent { */ @Deprecated public void setDatabase(String database) { - DAO.configOverwrites.put("hibernate.connection.url", - "jdbc:mysql://localhost/" + database); + DAO.database = database; } /** diff --git a/src/de/tud/kom/p2psim/impl/util/db/dao/CommitThread.java b/src/de/tud/kom/p2psim/impl/util/db/dao/CommitThread.java index a8ca5198eda5635413985b83c55ed1a4628db72e..00db374163cb9469ab0be8abd568ef9a687f64aa 100644 --- a/src/de/tud/kom/p2psim/impl/util/db/dao/CommitThread.java +++ b/src/de/tud/kom/p2psim/impl/util/db/dao/CommitThread.java @@ -25,6 +25,8 @@ import java.util.List; import javax.persistence.EntityManager; +import org.hibernate.Session; + import de.tud.kom.p2psim.impl.util.db.metric.CustomMeasurement; import de.tud.kom.p2psim.impl.util.db.metric.HostMetric; import de.tud.kom.p2psim.impl.util.db.metric.HostMetricBound; @@ -81,30 +83,30 @@ public class CommitThread implements Runnable { public boolean commitQueue() { int sessions = incNumSessions(); - EntityManager em = null; + Session s = null; boolean ok = true; try { - em = DAO.getEntityManager(); - em.getTransaction().begin(); + s = DAO.getSession(); + s.getTransaction().begin(); for (Object persist : persistQueue) { if (persist instanceof HostMetricBound) { HostMetric metric = ((HostMetricBound)persist).getHostMetric(); if (metric != null) { - HostMetric foundMetric = em.find(HostMetric.class, metric.getId()); + HostMetric foundMetric = (HostMetric) s.get(HostMetric.class, metric.getId()); if (foundMetric != null) { ((HostMetricBound) persist).setHostMetric(foundMetric); } } } // log.info("Persisting " + persist); - em.persist(persist); + s.persist(persist); if (persist instanceof CustomMeasurement) { ((CustomMeasurement)persist).afterPersist(); } } - em.getTransaction().commit(); - em.close(); + s.getTransaction().commit(); + s.close(); ok = true; } catch (org.hibernate.HibernateException e) { throw e; diff --git a/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java b/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java index fc4fb2ff32915ba02fd97f55630d678099d54709..90df47e8b8ea2045df8335a89661e4f172e165fc 100644 --- a/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java +++ b/src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java @@ -31,7 +31,23 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.hibernate.HibernateException; - +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.service.ServiceRegistry; +import org.omg.CORBA.CustomMarshal; + +import de.tud.kom.p2psim.impl.util.db.metric.CustomMeasurement; +import de.tud.kom.p2psim.impl.util.db.metric.Experiment; +import de.tud.kom.p2psim.impl.util.db.metric.HostMetric; +import de.tud.kom.p2psim.impl.util.db.metric.HostMetricBound; +import de.tud.kom.p2psim.impl.util.db.metric.Measurement; +import de.tud.kom.p2psim.impl.util.db.metric.MeasurementPair; +import de.tud.kom.p2psim.impl.util.db.metric.MeasurementPairList; +import de.tud.kom.p2psim.impl.util.db.metric.MeasurementSingle; +import de.tud.kom.p2psim.impl.util.db.metric.Metric; +import de.tud.kom.p2psim.impl.util.db.metric.MetricDescription; import de.tudarmstadt.maki.simonstrator.api.Monitor; import de.tudarmstadt.maki.simonstrator.api.Monitor.Level; @@ -49,88 +65,160 @@ import de.tudarmstadt.maki.simonstrator.api.Monitor.Level; */ public class DAO { - - /** - * The actually session for all DAOs - */ - private static final ThreadLocal entityManager = new ThreadLocal(); - - /** - * The session factory from Hibernate - */ - private static EntityManagerFactory emf; +// /** +// * The actually session for all DAOs +// */ +// private static final ThreadLocal entityManager = new ThreadLocal(); +// +// /** +// * The session factory from Hibernate +// */ +// private static EntityManagerFactory emf; private static final int QUEUE_SIZE = 20000; /** * The queue for the big amount of data */ - private static ArrayList persistQueue = new ArrayList(QUEUE_SIZE); + private static ArrayList persistQueue = new ArrayList( + QUEUE_SIZE); private static final LinkedList threads = new LinkedList(); private static int maxConnections = 1; - + private static long objectCount = 0; + private static long commitCount = 0; + private static long commitTime = 0; - public static Map configOverwrites = new HashMap(); +// public static Map configOverwrites = new HashMap(); + + private static ArrayList> daoClasses = new ArrayList>(); + + public static String database; + + public static String username; + + public static String password; + + private static SessionFactory sessionFactory; - public static ArrayList> daoClasses = new ArrayList>(); + private static final ThreadLocal session = new ThreadLocal(); /** - * Gets the session - * + * Hibernate-only implementation to support annotated classes without active + * binding + * * @return */ - public static EntityManager getEntityManager() { - EntityManager em = DAO.entityManager.get(); - if (emf == null) { - /* - * Setting hibernate properties here, to remove dependency on - * persistence.xml. Please note: set your database, user, and - * password in your XML-Config, using the MetricOutputDAO. - */ - Properties properties = new Properties(); - - // Disable output - properties.put("hibernate.show_sql", "false"); - properties.put("hibernate.format_sql", "false"); - // Connection-driver for mySQL - properties.put("hibernate.connection.driver_class", - "com.mysql.jdbc.Driver"); - // Default user/pass/database, overwrite using MetricOutputDAO - properties.put("hibernate.connection.url", ""); - properties.put("hibernate.connection.username", ""); - properties.put("hibernate.connection.password", ""); - - // mySQL 5 - properties.put("hibernate.dialect", - "org.hibernate.dialect.MySQL5InnoDBDialect"); - - properties.put("hibernate.transaction.factory_class", - "org.hibernate.transaction.JDBCTransactionFactory"); - properties.put("hibernate.pool_size", "1"); - properties.put("hibernate.hbm2ddl.auto", "update"); - properties.put("hibernate.current_session_context_class", "thread"); - - properties.put("hibernate.ejb.loaded.classes", daoClasses); - - // overwrite user, table, and pass - properties.putAll(configOverwrites); - - emf = Persistence - .createEntityManagerFactory("PeerfactSIM-EM", - properties); + public static Session getSession() { + Session currSession = DAO.session.get(); + if (sessionFactory == null) { + try { + Configuration cfg = new Configuration(); + /* + * TODO config + */ + cfg.setProperty("hibernate.show_sql", "false"); + cfg.setProperty("hibernate.format_sql", "false"); + cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); + // Default user/pass/database, overwrite using MetricOutputDAO + cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/"+database); + cfg.setProperty("hibernate.connection.username", username); + cfg.setProperty("hibernate.connection.password", password); + + // mySQL 5 + cfg.setProperty("hibernate.dialect", + "org.hibernate.dialect.MySQL5InnoDBDialect"); + + cfg.setProperty("hibernate.transaction.factory_class", + "org.hibernate.transaction.JDBCTransactionFactory"); + cfg.setProperty("hibernate.pool_size", "1"); + cfg.setProperty("hibernate.hbm2ddl.auto", "update"); + cfg.setProperty("hibernate.current_session_context_class", "thread"); + + // Add core classes + cfg.addAnnotatedClass(CustomMeasurement.class); + cfg.addAnnotatedClass(Experiment.class); + cfg.addAnnotatedClass(HostMetric.class); + cfg.addAnnotatedClass(HostMetricBound.class); + cfg.addAnnotatedClass(Measurement.class); + cfg.addAnnotatedClass(MeasurementPair.class); + cfg.addAnnotatedClass(MeasurementPairList.class); + cfg.addAnnotatedClass(MeasurementSingle.class); + cfg.addAnnotatedClass(Metric.class); + cfg.addAnnotatedClass(MetricDescription.class); + + for (Class clazz : daoClasses) { + cfg.addAnnotatedClass(clazz); + } + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() + .applySettings(cfg.getProperties()).build(); + sessionFactory = cfg.buildSessionFactory(serviceRegistry); + } catch (Throwable ex) { + throw new ExceptionInInitializerError(ex); + } } - if (em == null) { - em = emf.createEntityManager(); - DAO.entityManager.set(em); + if (currSession == null) { + currSession = sessionFactory.openSession(); + DAO.session.set(currSession); } - return em; + return currSession; } + /** + * Gets the session + * + * @return + */ +// public static EntityManager getEntityManager() { +// EntityManager em = DAO.entityManager.get(); +// if (emf == null) { +// /* +// * Setting hibernate properties here, to remove dependency on +// * persistence.xml. Please note: set your database, user, and +// * password in your XML-Config, using the MetricOutputDAO. +// */ +// Properties properties = new Properties(); +// +// // Disable output +// properties.put("hibernate.show_sql", "false"); +// properties.put("hibernate.format_sql", "false"); +// // Connection-driver for mySQL +// properties.put("hibernate.connection.driver_class", +// "com.mysql.jdbc.Driver"); +// // Default user/pass/database, overwrite using MetricOutputDAO +// properties.put("hibernate.connection.url", ""); +// properties.put("hibernate.connection.username", ""); +// properties.put("hibernate.connection.password", ""); +// +// // mySQL 5 +// properties.put("hibernate.dialect", +// "org.hibernate.dialect.MySQL5InnoDBDialect"); +// +// properties.put("hibernate.transaction.factory_class", +// "org.hibernate.transaction.JDBCTransactionFactory"); +// properties.put("hibernate.pool_size", "1"); +// properties.put("hibernate.hbm2ddl.auto", "update"); +// properties.put("hibernate.current_session_context_class", "thread"); +// +// properties.put("hibernate.ejb.loaded.classes", daoClasses); +// +// // overwrite user, table, and pass +// properties.putAll(configOverwrites); +// +// emf = Persistence.createEntityManagerFactory("PeerfactSIM-EM", +// properties); +// } +// if (em == null) { +// em = emf.createEntityManager(); +// DAO.entityManager.set(em); +// } +// return em; +// } + public static void addEntityClass(Class entity) { daoClasses.add(entity); } @@ -139,14 +227,16 @@ public class DAO { * begin the transaction */ protected static void begin() { - getEntityManager().getTransaction().begin(); + getSession().getTransaction().begin(); + // getEntityManager().getTransaction().begin(); } /** * commit the transaction */ protected static void commit() { - getEntityManager().getTransaction().commit(); + getSession().getTransaction().commit(); + // getEntityManager().getTransaction().commit(); } /** @@ -155,27 +245,32 @@ public class DAO { */ protected static void rollback() { try { - getEntityManager().getTransaction().rollback(); + // getEntityManager().getTransaction().rollback(); + getSession().getTransaction().rollback(); } catch (HibernateException e) { Monitor.log(DAO.class, Level.WARN, "The Rollback could not be executed! %s", e); } try { - getEntityManager().close(); + // getEntityManager().close(); + getSession().close(); } catch (HibernateException e) { Monitor.log(DAO.class, Level.WARN, "The session could not be closed! %s", e); } - DAO.entityManager.set(null); + // DAO.entityManager.set(null); + DAO.session.set(null); } /** * Close the session */ public static void close() { - getEntityManager().close(); - DAO.entityManager.set(null); + // getEntityManager().close(); + // DAO.entityManager.set(null); + getSession().close(); + DAO.session.set(null); } /** @@ -187,7 +282,8 @@ public class DAO { */ public static void persistImmediately(Object o) { begin(); - getEntityManager().persist(o); + // getEntityManager().persist(o); + getSession().persist(o); commit(); close(); } @@ -207,8 +303,8 @@ public class DAO { } /** - * Commit the queue in a background thread so the simulation does not have to - * wait for the database. + * Commit the queue in a background thread so the simulation does not have + * to wait for the database. * * To avoid any synchronization problems the entire queue is handed over to * the committing thread and a new one created in its place. @@ -216,7 +312,7 @@ public class DAO { public static void commitQueue() { if (persistQueue.isEmpty()) return; - + commitCount++; objectCount += persistQueue.size(); @@ -242,7 +338,7 @@ public class DAO { } Thread thread = new Thread(new CommitThread(persistQueue)); - thread.setName("CommitThread #"+commitCount); + thread.setName("CommitThread #" + commitCount); thread.start(); threads.add(thread); persistQueue = new ArrayList(QUEUE_SIZE); @@ -280,16 +376,17 @@ public class DAO { * * @param o * The same object, which is already used to persist. - * + * * @return The object attached to the persistence context */ public static T update(T o) { commitQueue(); begin(); - T objRef = getEntityManager().merge(o); + @SuppressWarnings("unchecked") + T objRef = (T) getSession().merge(o); commit(); close(); - + return objRef; }