aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2013-02-14 20:13:51 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2013-02-14 20:13:51 +1300
commit11b7037755c08654fba3b3781f83445244582d59 (patch)
tree4f06a6e4a82c6c24bf897decd7921c33a30824d3 /src
parent7d9d5a220e8f79ce1bc0d4b9931dc0c848e99c43 (diff)
downloadtrackermap-web-11b7037755c08654fba3b3781f83445244582d59.tar.gz
trackermap-web-11b7037755c08654fba3b3781f83445244582d59.tar.bz2
trackermap-web-11b7037755c08654fba3b3781f83445244582d59.zip
Proper use Java persistence (fix #13)
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/web/server/model/DataServiceImpl.java168
1 files changed, 102 insertions, 66 deletions
diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java
index 81130604..e48fa73c 100644
--- a/src/org/traccar/web/server/model/DataServiceImpl.java
+++ b/src/org/traccar/web/server/model/DataServiceImpl.java
@@ -23,6 +23,7 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.servlet.ServletException;
@@ -44,7 +45,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
private static final String PERSISTENCE_UNIT_RELEASE = "release";
private static final String ATTRIBUTE_USER = "user";
- private EntityManager entityManager;
+ private EntityManagerFactory entityManagerFactory;
@Override
public void init() throws ServletException {
@@ -59,12 +60,12 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
persistenceUnit = PERSISTENCE_UNIT_DEBUG;
}
- entityManager = Persistence.createEntityManagerFactory(persistenceUnit).createEntityManager();
+ entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit);
}
@Override
public void destroy() {
- entityManager.close();
+ entityManagerFactory.close();
super.destroy();
}
@@ -89,16 +90,21 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
@Override
public boolean login(String login, String password) {
- TypedQuery<User> query = entityManager.createQuery(
- "SELECT x FROM User x WHERE x.login = :login", User.class);
- query.setParameter("login", login);
- List<User> results = query.getResultList();
-
- if (!results.isEmpty() && password.equals(results.get(0).getPassword())) {
- setUser(results.get(0));
- return true;
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
+ try {
+ TypedQuery<User> query = entityManager.createQuery(
+ "SELECT x FROM User x WHERE x.login = :login", User.class);
+ query.setParameter("login", login);
+ List<User> results = query.getResultList();
+
+ if (!results.isEmpty() && password.equals(results.get(0).getPassword())) {
+ setUser(results.get(0));
+ return true;
+ }
+ return false;
+ } finally {
+ entityManager.close();
}
- return false;
}
@Override
@@ -109,19 +115,24 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
@Override
public boolean register(String login, String password) {
- User user = new User();
- user.setLogin(login);
- user.setPassword(password);
- entityManager.getTransaction().begin();
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
- entityManager.persist(user);
- entityManager.getTransaction().commit();
- } catch (RuntimeException e) {
- entityManager.getTransaction().rollback();
- throw e;
+ User user = new User();
+ user.setLogin(login);
+ user.setPassword(password);
+ entityManager.getTransaction().begin();
+ try {
+ entityManager.persist(user);
+ entityManager.getTransaction().commit();
+ setUser(user);
+ return true;
+ } catch (RuntimeException e) {
+ entityManager.getTransaction().rollback();
+ throw e;
+ }
+ } finally {
+ entityManager.close();
}
- setUser(user);
- return true;
}
@Override
@@ -134,72 +145,97 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
@Override
public Device addDevice(Device device) {
- User user = getUser();
- entityManager.getTransaction().begin();
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
- entityManager.persist(device);
- user.getDevices().add(device);
- entityManager.getTransaction().commit();
- } catch (RuntimeException e) {
- entityManager.getTransaction().rollback();
- throw e;
+ User user = getUser();
+ entityManager.getTransaction().begin();
+ try {
+ entityManager.persist(device);
+ user.getDevices().add(device);
+ entityManager.getTransaction().commit();
+ return device;
+ } catch (RuntimeException e) {
+ entityManager.getTransaction().rollback();
+ throw e;
+ }
+ } finally {
+ entityManager.close();
}
- return device;
}
@Override
public Device updateDevice(Device device) {
- entityManager.getTransaction().begin();
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
- device = entityManager.merge(device);
- entityManager.getTransaction().commit();
- } catch (RuntimeException e) {
- entityManager.getTransaction().rollback();
- throw e;
+ entityManager.getTransaction().begin();
+ try {
+ device = entityManager.merge(device);
+ entityManager.getTransaction().commit();
+ return device;
+ } catch (RuntimeException e) {
+ entityManager.getTransaction().rollback();
+ throw e;
+ }
+ } finally {
+ entityManager.close();
}
- return device;
}
@Override
public Device removeDevice(Device device) {
- User user = getUser();
- entityManager.getTransaction().begin();
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
- device = entityManager.merge(device);
- user.getDevices().remove(device);
- entityManager.remove(device);
- entityManager.getTransaction().commit();
- } catch (RuntimeException e) {
- entityManager.getTransaction().rollback();
-
+ User user = getUser();
+ entityManager.getTransaction().begin();
+ try {
+ device = entityManager.merge(device);
+ user.getDevices().remove(device);
+ entityManager.remove(device);
+ entityManager.getTransaction().commit();
+ return device;
+ } catch (RuntimeException e) {
+ entityManager.getTransaction().rollback();
+ throw e;
+ }
+ } finally {
+ entityManager.close();
}
- return device;
}
@Override
public List<Position> getPositions(Device device, Date from, Date to) {
- List<Position> positions = new LinkedList<Position>();
- TypedQuery<Position> query = entityManager.createQuery(
- "SELECT x FROM Position x WHERE x.device = :device AND x.time BETWEEN :from AND :to", Position.class);
- query.setParameter("device", device);
- query.setParameter("from", from);
- query.setParameter("to", to);
- positions.addAll(query.getResultList());
- return positions;
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
+ try {
+ List<Position> positions = new LinkedList<Position>();
+ TypedQuery<Position> query = entityManager.createQuery(
+ "SELECT x FROM Position x WHERE x.device = :device AND x.time BETWEEN :from AND :to", Position.class);
+ query.setParameter("device", device);
+ query.setParameter("from", from);
+ query.setParameter("to", to);
+ positions.addAll(query.getResultList());
+ return positions;
+ } finally {
+ entityManager.close();
+ }
}
@Override
public List<Position> getLatestPositions() {
- List<Position> positions = new LinkedList<Position>();
- User user = getUser();
- if (user.getDevices() != null && !user.getDevices().isEmpty()) {
- TypedQuery<Position> query = entityManager.createQuery(
- "SELECT x FROM Position x WHERE x.id IN (" +
- "SELECT y.latestPosition FROM Device y WHERE y IN (:devices))", Position.class);
- query.setParameter("devices", user.getDevices());
- positions.addAll(query.getResultList());
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
+ try {
+ List<Position> positions = new LinkedList<Position>();
+ User user = getUser();
+ if (user.getDevices() != null && !user.getDevices().isEmpty()) {
+ TypedQuery<Position> query = entityManager.createQuery(
+ "SELECT x FROM Position x WHERE x.id IN (" +
+ "SELECT y.latestPosition FROM Device y WHERE y IN (:devices))", Position.class);
+ query.setParameter("devices", user.getDevices());
+ positions.addAll(query.getResultList());
+ }
+ return positions;
+ } finally {
+ entityManager.close();
}
- return positions;
}
}