-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHibernateConnection.java
executable file
·95 lines (84 loc) · 3.57 KB
/
HibernateConnection.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package bonpay.partner.database.connector;
import java.sql.Connection;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/** êëàññ, êîòîðûé ñîçäàåò Hibernate.Session íà îñíîâàíèè Connection */
public class HibernateConnection {
private SessionFactory sessionFactory=null;
private IConnector connectorFactory;
/** ñîçäàòü íà áàçå Connection Hibernate íàäñòðîéêó
* @param connectorFactory - êîííåêòîð, êîòîðûé ìîæåò ãåíåðèðîâàòü Connection ê áàçå äàííûõ
* @param hibernateDialect - "org.hibernate.dialect.FirebirdDialect"
* ( example for HSQLDB - org.hsqldb.jdbcDriver)
* @throws âûáðàñûâàåò èñêëþ÷åíèå â ñëó÷àå, êîãäà íå óäàëîñü ñîçäàòü Hibernate
* */
public HibernateConnection(IConnector connectorFactory, String hibernateDialect,Class<?> ... classes) throws Exception{
this.connectorFactory=connectorFactory;
sessionFactory=getAnnotationConfiguration(hibernateDialect,classes).buildSessionFactory();
}
private AnnotationConfiguration getAnnotationConfiguration(String hibernateDialect,Class<?> ... classes){
AnnotationConfiguration aconf = new AnnotationConfiguration();
Properties properties=new Properties();
properties.put("hibernate.dialect", hibernateDialect);
properties.put("hibernate.connection.driver_class", "org.firebirdsql.jdbc.FBDriver");
//properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:baseball");
//properties.put("hibernate.connection.username", "sa");
//properties.put("hibernate.connection.password", "");
properties.put("hibernate.connection.pool_size", "10");
properties.put("hibernate.connection.autocommit", "false");
//properties.put("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
//properties.put("hibernate.hbm2ddl.auto", "create-drop");
//properties.put("hibernate.show_sql", "true");
//properties.put("hibernate.connection.datasource", "java:comp/env/jdbc/test");
//hibernate.connection.datasource datasource JNDI name
//hibernate.jndi.url URL of the JNDI provider (optional)
//hibernate.jndi.class class of the JNDI InitialContextFactory (optional)
//properties.put("hibernate.c3p0.min_size","5");
//properties.put("hibernate.c3p0.max_size","20");
//properties.put("hibernate.c3p0.timeout","1800");
//properties.put("hibernate.c3p0.max_statements","50");
aconf.setProperties(properties);
for(int counter=0;counter<classes.length;counter++){
aconf.addAnnotatedClass(classes[counter]);
}
return aconf;
}
/** get Hibernate Session */
public Session openSession(Connection connection){
try{
return sessionFactory.openSession(connection);
}catch(Exception ex){
System.err.println("HibernateConnection GetConnection Exception: "+ex.getMessage());
return null;
}
}
/** get Hibernate Session */
/* public Session openSession(Connection connection){
try{
return sessionFactory.openSession(this.connection);
}catch(Exception ex){
System.err.println("openSession Exception: "+ex.getMessage());
return null;
}
}
*/
/** get Connection */
public Connection getConnection(){
Connection connection=this.connectorFactory.getConnection();
try{
connection.setAutoCommit(false);
}catch(Exception ex){};
return connection;
}
/** çàêðûòü âñå ñîåäèíåíèÿ ñ áàçîé äàííûõ */
public void close(){
System.out.println("close");
this.sessionFactory.close();
this.connectorFactory.closeAllConnection();
}
public void finalize(){
this.close();
}
}