svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.sqlite / org.gvsig.sqlite.provider / src / main / java / org / gvsig / sqlite / dal / SQLiteJDBCConnection.java @ 47606
History | View | Annotate | Download (5.58 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.sqlite.dal; |
7 |
|
8 |
import java.sql.Connection; |
9 |
import java.sql.PreparedStatement; |
10 |
import java.sql.SQLException; |
11 |
import java.sql.Statement; |
12 |
import java.util.HashMap; |
13 |
import java.util.Map; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.spi.DataTransactionServices; |
16 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
17 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
18 |
import org.gvsig.fmap.dal.store.jdbc2.spi.ConnectionProvider; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCConnectionBase; |
20 |
import static org.gvsig.sqlite.dal.SQLiteJDBCConnection.LOGGER; |
21 |
import org.slf4j.Logger; |
22 |
import org.slf4j.LoggerFactory; |
23 |
|
24 |
/**
|
25 |
*
|
26 |
* @author fdiaz
|
27 |
*/
|
28 |
public class SQLiteJDBCConnection extends JDBCConnectionBase { |
29 |
|
30 |
public static Logger LOGGER = LoggerFactory.getLogger(SQLiteJDBCConnection.class); |
31 |
|
32 |
private static Map<String, SQLiteJDBCConnection> connections = new HashMap<>(); |
33 |
|
34 |
// private static Stack transactions = new Stack();
|
35 |
|
36 |
// private int references = 0;
|
37 |
|
38 |
public static JDBCConnection create(SQLiteHelper helper) throws SQLException { |
39 |
SQLiteConnectionParameters connectionParameters = helper.getConnectionParameters(); |
40 |
//
|
41 |
String key = helper.getConnectionProviderKey(connectionParameters);
|
42 |
// SQLiteJDBCConnection conn = connections.getOrDefault(key, null);
|
43 |
// if(conn != null){
|
44 |
// conn.references++;
|
45 |
// LOGGER.info("references = "+conn.references+" trans "+conn.isInTransaction());
|
46 |
// return conn;
|
47 |
// }
|
48 |
SQLiteJDBCConnection conn = new SQLiteJDBCConnection(
|
49 |
helper.getTransaction(), |
50 |
helper.getConnectionProvider().getConnection(), |
51 |
key |
52 |
); |
53 |
connections.put(key, conn); |
54 |
// LOGGER.info("new connection references = "+conn.references+" trans "+conn.isInTransaction());
|
55 |
return conn;
|
56 |
|
57 |
} |
58 |
|
59 |
public SQLiteJDBCConnection(DataTransactionServices transaction, Connection conn, String id) { |
60 |
super(transaction, conn, id);
|
61 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] createConnection() "+isInDataTransaction()); |
62 |
} |
63 |
|
64 |
public SQLiteJDBCConnection(DataTransactionServices transaction,Connection conn, String id, ConnectionProvider connectionProvider) { |
65 |
super(transaction, conn, id, connectionProvider);
|
66 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] createConnection() "+isInDataTransaction()); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public void close() throws SQLException { |
71 |
if( this.transaction==null ) { |
72 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] closeConnection() "+isInDataTransaction()); |
73 |
} |
74 |
|
75 |
// this.references--;
|
76 |
// LOGGER.info("Close references = "+this.references+" trans "+this.isInTransaction());
|
77 |
// if(this.references <= 0){
|
78 |
// super.close();
|
79 |
// connections.remove(this.getId());
|
80 |
// } else {
|
81 |
// this.abort();
|
82 |
// }
|
83 |
super.close();
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public synchronized void begin() throws SQLException { |
88 |
if(!isInTransaction()){
|
89 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] begin() "+isInDataTransaction()); |
90 |
} |
91 |
// if(!transactions.isEmpty()){
|
92 |
// Object mon = new Object();
|
93 |
// synchronized(mon){
|
94 |
// try {
|
95 |
// transactions.push(mon);
|
96 |
// mon.wait();
|
97 |
// } catch (InterruptedException ex) {
|
98 |
// throw new RuntimeException("", ex);
|
99 |
// }
|
100 |
// }
|
101 |
// }
|
102 |
super.begin();
|
103 |
} |
104 |
|
105 |
@Override
|
106 |
public synchronized void commit() throws SQLException { |
107 |
if( !(this.transaction!=null && this.transaction.isInProgress() )) { |
108 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] commit() "+isInDataTransaction()); |
109 |
} |
110 |
// if(!transactions.isEmpty()){
|
111 |
// Object mon = transactions.pop();
|
112 |
// mon.notify();
|
113 |
//
|
114 |
// }
|
115 |
super.commit();
|
116 |
} |
117 |
|
118 |
@Override
|
119 |
public void abort() throws DataException { |
120 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] abort() "+isInDataTransaction()); |
121 |
super.abort();
|
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void finish() throws DataException { |
126 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] finish() "+isInDataTransaction()); |
127 |
super.finish();
|
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void execute(String sql) throws SQLException { |
132 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] execute("+sql+") "+isInDataTransaction()); |
133 |
super.execute(sql);
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public PreparedStatement prepareStatement(String sql) throws SQLException { |
138 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] prepareStatement("+sql+") "+isInDataTransaction()); |
139 |
return super.prepareStatement(sql); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public void rollback() throws SQLException { |
144 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] rollback() "+isInDataTransaction()); |
145 |
super.rollback();
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public Statement createStatement() throws SQLException { |
150 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] createStatement() "+isInDataTransaction()); |
151 |
return super.createStatement(); |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public Statement createStatement(String sql) throws SQLException { |
156 |
LOGGER.info("["+JDBCUtils.getConnId(conn)+"] createStatement("+sql+") "+isInDataTransaction()); |
157 |
return super.createStatement(sql); |
158 |
} |
159 |
|
160 |
} |