Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Simonstrator
PeerfactSim.KOM
Commits
970dcd7e
Commit
970dcd7e
authored
Jul 01, 2015
by
Björn Richerzhagen
Browse files
Got rid of the persistence.xml - now all configured programmatically
parent
86bce24a
Changes
7
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
970dcd7e
...
...
@@ -269,26 +269,18 @@
<artifactId>
mysql-connector-java
</artifactId>
<version>
5.1.14
</version>
</dependency>
<dependency>
<groupId>
org.hibernate
</groupId>
<artifactId>
hibernate-entitymanager
</artifactId>
<version>
4.1.1.Final
</version>
</dependency>
<dependency>
<groupId>
javax.transaction
</groupId>
<artifactId>
jta
</artifactId>
<version>
1.1
</version>
</dependency>
<dependency>
<groupId>
org.hibernate.javax.persistence
</groupId>
<artifactId>
hibernate-jpa-2.0-api
</artifactId>
<version>
1.0.1.Final
</version>
<artifactId>
hibernate-core
</artifactId>
<version>
4.3.10.Final
</version>
</dependency>
<dependency>
<!-- <dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
</dependency>
-->
<!-- Glab Database -->
<dependency>
...
...
src/META-INF/persistence.xml
deleted
100644 → 0
View file @
86bce24a
<persistence
xmlns=
"http://java.sun.com/xml/ns/persistence"
version=
"1.0"
>
<persistence-unit
name=
"PeerfactSIM-EM"
transaction-type=
"RESOURCE_LOCAL"
>
<properties>
<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"
/>
<!-- DO NOT SET url, password and user HERE!
Instead, use the MetricOutputDAO-class to set those values in your config! -->
<property
name=
"hibernate.connection.url"
value=
""
/>
<property
name=
"hibernate.connection.username"
value=
""
/>
<property
name=
"hibernate.connection.password"
value=
""
/>
<property
name=
"hibernate.dialect"
value=
"org.hibernate.dialect.MySQL5InnoDBDialect"
/>
<property
name=
"hibernate.transaction.factory_class"
value=
"org.hibernate.transaction.JDBCTransactionFactory"
/>
<property
name=
"hibernate.pool_size"
value=
"1"
/>
<property
name=
"hibernate.hbm2ddl.auto"
value=
"update"
/>
<property
name=
"hibernate.current_session_context_class"
value=
"thread"
/>
</properties>
</persistence-unit>
</persistence>
src/de/tud/kom/p2psim/impl/analyzer/metric/output/MetricOutputDAO.java
View file @
970dcd7e
...
...
@@ -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
)
{
...
...
src/de/tud/kom/p2psim/impl/common/DefaultMonitor.java
View file @
970dcd7e
...
...
@@ -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
;
}
}
...
...
src/de/tud/kom/p2psim/impl/simengine/Simulator.java
View file @
970dcd7e
...
...
@@ -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
;
}
/**
...
...
src/de/tud/kom/p2psim/impl/util/db/dao/CommitThread.java
View file @
970dcd7e
...
...
@@ -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
.
get
EntityManager
();
em
.
getTransaction
().
begin
();
s
=
DAO
.
get
Session
();
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
;
...
...
src/de/tud/kom/p2psim/impl/util/db/dao/DAO.java
View file @
970dcd7e
...
...
@@ -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
>
entityManager
=
new
ThreadLocal
<
EntityManager
>();
/**
* The session factory from Hibernate
*/
private
static
EntityManagerFactory
emf
;
// /**
// * The actually session for all DAOs
// */
// private static final ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>();
//
// /**
// * 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
<
Object
>
persistQueue
=
new
ArrayList
<
Object
>(
QUEUE_SIZE
);
private
static
ArrayList
<
Object
>
persistQueue
=
new
ArrayList
<
Object
>(
QUEUE_SIZE
);
private
static
final
LinkedList
<
Thread
>
threads
=
new
LinkedList
<
Thread
>();
private
static
int
maxConnections
=
1
;
private
static
long
objectCount
=
0
;
private
static
long
commitCount
=
0
;
private
static
long
commitTime
=
0
;
public
static
Map
<
String
,
Object
>
configOverwrites
=
new
HashMap
<
String
,
Object
>();
// public static Map<String, Object> configOverwrites = new HashMap<String, Object>();
private
static
ArrayList
<
Class
<?>>
daoClasses
=
new
ArrayList
<
Class
<?>>();
public
static
String
database
;
public
static
String
username
;
public
static
String
password
;
private
static
SessionFactory
sessionFactory
;
p
ublic
static
ArrayList
<
Class
<?>>
daoClasses
=
new
ArrayList
<
Class
<?>
>();
p
rivate
static
final
ThreadLocal
<
Session
>
session
=
new
ThreadLocal
<
Session
>();
/**
* 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
<
Object
>(
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
>
T
update
(
T
o
)
{
commitQueue
();
begin
();
T
objRef
=
getEntityManager
().<
T
>
merge
(
o
);
@SuppressWarnings
(
"unchecked"
)
T
objRef
=
(
T
)
getSession
().
merge
(
o
);
commit
();
close
();
return
objRef
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment