Skip to content

Commit

Permalink
Allow changing logger level
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Rua committed Apr 23, 2015
1 parent 440cb1c commit b462074
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ If you use Maven to manage your dependencies, Orianna Hibernate is posted on Mav
<dependency>
<groupId>com.robrua</groupId>
<artifactId>orianna-hibernate</artifactId>
<version>2.2.5</version>
<version>2.2.6</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>orianna-hibernate</artifactId>
<version>2.2.6-SNAPSHOT</version>
<version>2.2.6</version>
<packaging>jar</packaging>

<scm>
Expand All @@ -28,7 +28,7 @@
<dependency>
<groupId>com.robrua</groupId>
<artifactId>orianna</artifactId>
<version>2.2.6-SNAPSHOT</version>
<version>2.2.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
38 changes: 33 additions & 5 deletions src/com/robrua/orianna/store/HibernateDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.Criteria;
import org.hibernate.ScrollMode;
Expand Down Expand Up @@ -37,6 +39,7 @@ public static class Builder {
private String dialect = "org.hibernate.dialect.MySQLDialect";
private String driver = "com.mysql.jdbc.Driver";
private int entityClearTheshold = 100;
private Level logLevel = Level.INFO;
private String password = null;
private boolean showSQL = false;
private String url = null;
Expand Down Expand Up @@ -65,6 +68,9 @@ public HibernateDB build() {
throw new IllegalArgumentException("URL, Username, and Password must be set!");
}

final Logger logger = Logger.getLogger("org.hibernate");
logger.setLevel(logLevel);

final Configuration configuration = new Configuration();

// DB configuration
Expand Down Expand Up @@ -125,6 +131,16 @@ public Builder entityClearThreshold(final int entityClearTheshold) {
return this;
}

/**
* @param logLevel
* hibernate log level
* @return the builder
*/
public Builder logLevel(final Level logLevel) {
this.logLevel = logLevel;
return this;
}

/**
* @param password
* hibernate.connection.password
Expand Down Expand Up @@ -167,9 +183,10 @@ public Builder username(final String username) {
}

private class DBIterator<T extends OriannaObject<?>> extends CloseableIterator<T> {
private final boolean hasAny;
private boolean isClosed = false;
private final ScrollableResults result;
private final Class<T> type;
private final boolean hasAny;

/**
* @param type
Expand All @@ -186,12 +203,23 @@ public DBIterator(final Class<T> type, final ScrollableResults result) {

@Override
public void close() {
result.close();
if(!isClosed) {
result.close();
isClosed = true;
}
}

@Override
public boolean hasNext() {
return hasAny && !result.isLast();
if(isClosed) {
return false;
}

final boolean hasNext = hasAny && !result.isLast();
if(!hasNext && !isClosed) {
close();
}
return hasNext;
}

@SuppressWarnings("unchecked")
Expand All @@ -200,7 +228,7 @@ public T next() {
if(!hasNext()) {
return null;
}

result.next();
try {
return (T)type.getDeclaredConstructors()[0].newInstance(result.get(0));
Expand Down Expand Up @@ -265,7 +293,7 @@ private static String getIndexRow(final Class<? extends OriannaDto> clazz, final
*/
public HibernateDB(final Configuration cfg, final int entityClearTheshold) {
this.entityClearTheshold = entityClearTheshold;

// Add DTO classes
cfg.addAnnotatedClass(com.robrua.orianna.type.dto.champion.Champion.class).addAnnotatedClass(com.robrua.orianna.type.dto.champion.ChampionList.class)
.addAnnotatedClass(com.robrua.orianna.type.dto.currentgame.BannedChampion.class)
Expand Down
117 changes: 61 additions & 56 deletions src/com/robrua/orianna/store/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,90 @@
import org.hibernate.SessionFactory;

/**
* Handles multiple threads so that multiple threads can safely access the database. Automatically closes sessions from terminated threads.
*
* Handles multiple threads so that multiple threads can safely access the
* database. Automatically closes sessions from terminated threads.
*
* @author Rob Rua (robrua@alumni.cmu.edu)
*/
public class SessionManager implements Closeable {
private final Map<Thread, Session> sessions;
private final SessionFactory factory;
private final long checkMillis;
private final Cleaner cleaner;

/**
* @param factory the session factory
* @param checkMillis how often to check for terminated threads and close their sessions
*/
public SessionManager(SessionFactory factory, long checkMillis) {
this.factory = factory;
this.sessions = new ConcurrentHashMap<>();
this.checkMillis = checkMillis;
this.cleaner = new Cleaner();
new Thread(cleaner).start();
}

/**
* @return the hibernate session for the current thread
*/
public Session getSession() {
Thread thread = Thread.currentThread();
Session session = sessions.get(thread);
if(session == null) {
session = factory.openSession();
sessions.put(thread, session);
}

return session;
}

@Override
public void close() {
cleaner.stop();
for(Session session : sessions.values()) {
session.close();
}
factory.close();
}

/**
* Closes sessions from terminated threads
*/
private class Cleaner implements Runnable {
private class Cleaner implements Runnable {
private volatile boolean stopped = false;
private Thread thread;

/**
* Stops the cleaner
*/
public void stop() {
stopped = true;
thread.interrupt();
}


@Override
public void run() {
thread = Thread.currentThread();

while(!stopped) {
for(Thread thread : sessions.keySet()) {
for(final Thread thread : sessions.keySet()) {
if(!thread.isAlive()) {
sessions.get(thread).close();
sessions.remove(thread);
}
}

try {
Thread.sleep(checkMillis);
}
catch(InterruptedException e) {
catch(final InterruptedException e) {
continue;
}
}
}

/**
* Stops the cleaner
*/
public void stop() {
stopped = true;
thread.interrupt();
}
}

private final long checkMillis;
private final Cleaner cleaner;
private final SessionFactory factory;

private final Map<Thread, Session> sessions;

/**
* @param factory
* the session factory
* @param checkMillis
* how often to check for terminated threads and close their
* sessions
*/
public SessionManager(final SessionFactory factory, final long checkMillis) {
this.factory = factory;
sessions = new ConcurrentHashMap<>();
this.checkMillis = checkMillis;
cleaner = new Cleaner();
new Thread(cleaner).start();
}

@Override
public void close() {
cleaner.stop();
for(final Session session : sessions.values()) {
session.close();
}
factory.close();
}

/**
* @return the hibernate session for the current thread
*/
public Session getSession() {
final Thread thread = Thread.currentThread();
Session session = sessions.get(thread);
if(session == null) {
session = factory.openSession();
sessions.put(thread, session);
}

return session;
}
}

0 comments on commit b462074

Please sign in to comment.