|
1 | 1 | package refactoringml.db;
|
2 | 2 |
|
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.SQLException; |
| 5 | + |
3 | 6 | import org.apache.logging.log4j.LogManager;
|
4 | 7 | import org.apache.logging.log4j.Logger;
|
5 | 8 | import org.hibernate.Session;
|
6 |
| -import org.hibernate.SessionFactory; |
| 9 | +import org.hibernate.Transaction; |
7 | 10 | import org.hibernate.TransactionException;
|
8 | 11 | import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
9 |
| -import java.sql.Connection; |
10 |
| -import java.sql.SQLException; |
11 | 12 |
|
12 | 13 | public class Database {
|
13 |
| - private SessionFactory sf; |
14 |
| - private Session session; |
| 14 | + private final Session session; |
15 | 15 |
|
16 | 16 | private static final Logger log = LogManager.getLogger(Database.class);
|
17 | 17 |
|
18 |
| - public Database(SessionFactory sf) { |
19 |
| - this.sf = sf; |
20 |
| - } |
21 |
| - |
22 |
| - public void openSession() { |
23 |
| - // if there's an open session, let's close it first. |
24 |
| - // this should not happen, though, only due to bad logic |
25 |
| - if(this.session!=null) { |
26 |
| - log.error("Session is open, but we are opening another one!"); |
27 |
| - close(); |
28 |
| - } |
29 |
| - |
30 |
| - this.session = sf.openSession(); |
31 |
| - session.beginTransaction(); |
| 18 | + public Database(Session session) { |
| 19 | + this.session = session; |
32 | 20 | }
|
33 | 21 |
|
34 |
| - //shutdown the session factory and all connections |
35 |
| - public void shutdown(){ |
36 |
| - close(); |
37 |
| - sf.close(); |
38 |
| - sf = null; |
| 22 | + public void close() { |
| 23 | + session.close(); |
39 | 24 | }
|
40 | 25 |
|
41 |
| - public void commit() { |
42 |
| - this.session.getTransaction().commit(); |
| 26 | + public Transaction beginTransaction() { |
| 27 | + return session.beginTransaction(); |
43 | 28 | }
|
44 | 29 |
|
45 | 30 | public void persist(Object obj) {
|
46 | 31 | session.persist(obj);
|
47 | 32 | }
|
48 | 33 |
|
49 |
| - //Handles all the logic to persist an object to the database |
50 |
| - public void persistComplete(Object obj){ |
51 |
| - openSession(); |
52 |
| - persist(obj); |
53 |
| - commit(); |
54 |
| - close(); |
| 34 | + public <T> T merge(T toMerge) { |
| 35 | + return (T) session.merge(toMerge); |
55 | 36 | }
|
56 | 37 |
|
57 |
| - //Handles all the logic to update an object to the database |
58 |
| - public void updateComplete(Object obj){ |
59 |
| - openSession(); |
60 |
| - update(obj); |
61 |
| - commit(); |
62 |
| - close(); |
| 38 | + public void persistComplete(Object toPersist) { |
| 39 | + Transaction t = beginTransaction(); |
| 40 | + persist(toPersist); |
| 41 | + t.commit(); |
63 | 42 | }
|
64 | 43 |
|
65 |
| - public void update(Object obj) { |
66 |
| - session.update(obj); |
67 |
| - } |
| 44 | + public <T> T mergeComplete(T toMerge) { |
| 45 | + Transaction t = beginTransaction(); |
| 46 | + T merged = merge(toMerge); |
| 47 | + t.commit(); |
| 48 | + return merged; |
68 | 49 |
|
69 |
| - public void close() { |
70 |
| - try { |
71 |
| - if (session != null) |
72 |
| - session.close(); |
73 |
| - } catch(Exception e) { |
74 |
| - // what to do? this really shouldn't happen. |
75 |
| - log.error("Error when closing the connection to the Database: ", e); |
76 |
| - } finally { |
77 |
| - this.session = null; |
78 |
| - } |
79 | 50 | }
|
80 | 51 |
|
81 | 52 | public boolean projectExists(String gitUrl) {
|
82 |
| - Session shortSession = sf.openSession(); |
83 |
| - boolean exists = shortSession.createQuery("from Project p where p.gitUrl = :gitUrl") |
84 |
| - .setParameter("gitUrl", gitUrl) |
85 |
| - .list().size() > 0; |
86 |
| - shortSession.close(); |
87 |
| - |
88 |
| - return exists; |
| 53 | + return !session.createQuery("from Project p where p.gitUrl = :gitUrl").setParameter("gitUrl", gitUrl).list() |
| 54 | + .isEmpty(); |
89 | 55 | }
|
90 | 56 |
|
91 |
| - public long findAllRefactoringCommits(long projectId) { return findAllInstances("RefactoringCommit", projectId); } |
| 57 | + public long findAllRefactoringCommits(long projectId) { |
| 58 | + return findAllInstances("RefactoringCommit", projectId); |
| 59 | + } |
92 | 60 |
|
93 |
| - public long findAllStableCommits(long projectId) { return findAllInstances("StableCommit", projectId); } |
| 61 | + public long findAllStableCommits(long projectId) { |
| 62 | + return findAllInstances("StableCommit", projectId); |
| 63 | + } |
94 | 64 |
|
95 |
| - private long findAllInstances(String instance, long projectId){ |
96 |
| - Session shortSession = sf.openSession(); |
| 65 | + private long findAllInstances(String instance, long projectId) { |
97 | 66 | String query = "Select count(*) From " + instance + " where " + instance + ".project_id = " + projectId;
|
98 |
| - Object result = shortSession.createSQLQuery(query).getSingleResult(); |
99 |
| - shortSession.close(); |
| 67 | + Object result = session.createSQLQuery(query).getSingleResult(); |
100 | 68 | return Long.parseLong(result.toString());
|
101 | 69 | }
|
102 | 70 |
|
103 | 71 | public long findAllStableCommits(long projectId, int level) {
|
104 |
| - Session shortSession = sf.openSession(); |
105 |
| - String query = "Select count(*) From StableCommit where StableCommit.project_id = " + projectId + " AND StableCommit.commitThreshold = " + level; |
106 |
| - Object result = shortSession.createSQLQuery(query).getSingleResult(); |
107 |
| - shortSession.close(); |
| 72 | + String query = "Select count(*) From StableCommit where StableCommit.project_id = " + projectId |
| 73 | + + " AND StableCommit.commitThreshold = " + level; |
| 74 | + Object result = session.createSQLQuery(query).getSingleResult(); |
108 | 75 | return Long.parseLong(result.toString());
|
109 | 76 | }
|
110 | 77 |
|
111 |
| - //safely rollback a transaction with the db |
112 |
| - public void rollback(String logExtension) { |
113 |
| - //this session object itself should never be null, thus we don't check for it |
114 |
| - //nothing to do in this case, recovering the session or transaction is to much effort and the db takes care of a failed transaction |
115 |
| - if(!session.isOpen()) { |
| 78 | + // safely rollback a transaction with the db |
| 79 | + public void rollback(Transaction t,String logExtension) { |
| 80 | + // this session object itself should never be null, thus we don't check for it |
| 81 | + // nothing to do in this case, recovering the session or transaction is to much |
| 82 | + // effort and the db takes care of a failed transaction |
| 83 | + if (!session.isOpen()) { |
116 | 84 | log.error("Session was already closed during attempted rollback: Doing Nothing." + logExtension);
|
117 | 85 | return;
|
118 | 86 | }
|
119 | 87 |
|
120 |
| - if(!session.isConnected()){ |
121 |
| - try{ |
122 |
| - Connection connection = sf.getSessionFactoryOptions().getServiceRegistry(). |
123 |
| - getService(ConnectionProvider.class).getConnection(); |
| 88 | + if (!session.isConnected()) { |
| 89 | + try { |
| 90 | + Connection connection = session.getSessionFactory().getSessionFactoryOptions().getServiceRegistry() |
| 91 | + .getService(ConnectionProvider.class).getConnection(); |
124 | 92 | session.reconnect(connection);
|
125 | 93 | } catch (SQLException e) {
|
126 | 94 | log.error("Failed to reconnect session object." + logExtension, e);
|
127 | 95 | }
|
128 | 96 | }
|
129 | 97 |
|
130 |
| - //standard case for a rollback |
131 |
| - if(session.isConnected() && session.getTransaction() != null) { |
132 |
| - try{ |
133 |
| - session.getTransaction().rollback(); |
134 |
| - return; |
| 98 | + // standard case for a rollback |
| 99 | + if (session.isConnected() && session.getTransaction() != null) { |
| 100 | + try { |
| 101 | + t.rollback(); |
135 | 102 | } catch (TransactionException e) {
|
136 | 103 | log.error("Failed to rollback session: " + session.toString() + logExtension, e);
|
137 | 104 | }
|
138 | 105 | } else {
|
139 |
| - //other cases: |
140 |
| - //1. not connected to the DB : we could raise an error here, because something is probably wrong with the db |
141 |
| - //2. connected but no transaction object : nothing to do |
| 106 | + // other cases: |
| 107 | + // 1. not connected to the DB : we could raise an error here, because something |
| 108 | + // is probably wrong with the db |
| 109 | + // 2. connected but no transaction object : nothing to do |
142 | 110 | log.error("Session is in a bad state: " + session.toString() + logExtension);
|
143 | 111 | }
|
144 | 112 | }
|
|
0 commit comments