-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ManagedHibernateDBConnection.java
61 lines (50 loc) · 1.5 KB
/
ManagedHibernateDBConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.core;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
/**
* Implementing class for the MANAGED Context state for {@link HibernateDBConnection}
*/
public class ManagedHibernateDBConnection extends HibernateDBConnection {
@Autowired(required = true)
@Qualifier("managedSessionFactory")
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Override
public Session getSession() {
if (session == null) {
this.session = getSessionFactory().openSession();
}
return session;
}
@Override
public Session getCurrentSession() {
return getSession();
}
@Override
public Transaction getTransaction() {
if (transaction == null) {
this.transaction = getSession().beginTransaction();
}
return this.transaction;
}
@Override
public void commit() throws SQLException {
super.commit();
transaction = null;
}
}