Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / JDBCConnectionBase.java @ 47779

History | View | Annotate | Download (6.44 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.fmap.dal.store.jdbc2.spi;
7

    
8
import java.sql.Connection;
9
import java.sql.DatabaseMetaData;
10
import java.sql.PreparedStatement;
11
import java.sql.SQLException;
12
import java.sql.Statement;
13
import org.gvsig.fmap.dal.exception.DataException;
14
import org.gvsig.fmap.dal.spi.DataTransactionServices;
15
import org.gvsig.fmap.dal.store.jdbc.exception.SQLRuntimeException;
16
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
17
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

    
21
/**
22
 *
23
 * @author jjdelcerro
24
 */
25
public class JDBCConnectionBase implements JDBCConnection {
26

    
27
    protected static final Logger LOGGER = LoggerFactory.getLogger(JDBCConnectionBase.class);
28
    protected Connection conn;
29
    protected DataTransactionServices transaction;
30
    protected final String id;
31
    protected final ConnectionProvider connectionProvider;
32

    
33
    protected static class DataSQLException extends DataException {
34
        
35
        public DataSQLException(SQLException cause) {
36
            super(cause.getMessage(), cause, "_SQLException_"+cause.getSQLState(), 111);
37
        }
38
    }
39
    
40
    public JDBCConnectionBase(DataTransactionServices transaction,Connection conn, String id) {
41
        this(transaction, conn, id, null);
42
    }
43
    
44
    public JDBCConnectionBase(DataTransactionServices transaction,Connection conn, String id, ConnectionProvider connectionProvider) {
45
        this.id = id;
46
        this.transaction = transaction;
47
        this.conn = conn;
48
        if( this.transaction!=null ) {
49
            this.transaction.addConnection(this);
50
        }
51
        this.connectionProvider = connectionProvider;
52
        if( LOGGER.isDebugEnabled() ) {
53
            if( this.connectionProvider!=null ) {
54
                LOGGER.debug("CONN INIT  "+JDBCUtils.getConnId(this.conn)+" trans: "+JDBCUtils.getHexId(this.transaction)+" status: "+this.connectionProvider.getStatus());
55
            } else {
56
                LOGGER.debug("CONN INIT  "+JDBCUtils.getConnId(this.conn)+" trans: "+JDBCUtils.getHexId(this.transaction)+" status: UNKNOWN");
57
            }
58
        }
59
    }
60
    
61
    @Override
62
    public String getId() {
63
        return this.id;
64
    }
65
    
66
    @Override
67
    public void close() throws SQLException {
68
        String connid = JDBCUtils.getConnId(this.conn);
69
        if( this.transaction!=null ) {
70
            if( LOGGER.isDebugEnabled() ) {
71
                if( this.connectionProvider==null ) {
72
                    LOGGER.debug("CONN SKIP CLOSE "+connid+" trans: "+JDBCUtils.getHexId(this.transaction)+" status: UNKNOWN");
73
                } else {
74
                    LOGGER.debug("CONN SKIP CLOSE "+connid+" trans: "+JDBCUtils.getHexId(this.transaction)+" status: "+this.connectionProvider.getStatus());
75
                }
76
            }
77
            return;
78
        }
79
        this.conn.close();        
80
        if( LOGGER.isDebugEnabled() ) {
81
            if( this.connectionProvider==null ) {
82
                LOGGER.debug("CONN CLOSE "+connid+" trans: NULL status: UNKNOWN");
83
            } else {
84
                LOGGER.debug("CONN CLOSE "+connid+" trans: NULL status: "+this.connectionProvider.getStatus());
85
            }
86
        }
87
    }
88

    
89
    @Override
90
    public void closeQuietly() {
91
        try {
92
            this.close();
93
        } catch (SQLException ex) {
94
            LOGGER.debug("",ex);
95
        }
96
    }
97

    
98
    @Override
99
    public boolean isInTransaction() {
100
        try {
101
            return this.conn.getAutoCommit()==false;
102
        } catch (SQLException ex) {
103
            throw new SQLRuntimeException(ex);
104
        }
105
    }
106

    
107
    @Override
108
    public boolean isInDataTransaction() {
109
        return (this.transaction != null && transaction.isInProgress());
110
    }
111

    
112
    @Override
113
    public void begin() throws SQLException {
114
        if( this.conn.getAutoCommit() ) {
115
            this.conn.setAutoCommit(false);
116
        }
117
    }
118

    
119
    @Override
120
    public void rollback() throws SQLException {
121
        if( this.transaction!=null && this.transaction.isInProgress()) {
122
            return;
123
        }
124
        this.conn.rollback();
125
        this.conn.setAutoCommit(true);
126
    }
127

    
128
    @Override
129
    public void commit() throws SQLException {
130
        if( this.transaction!=null && this.transaction.isInProgress() ) {
131
            return;
132
        }
133
        this.conn.commit();
134
        this.conn.setAutoCommit(true);
135
    }
136

    
137
    @Override
138
    public Connection get() {
139
        return this.conn;
140
    }
141

    
142
    @Override
143
    public void execute(String sql) throws SQLException {
144
        JDBCUtils.execute(conn, sql);
145
    }
146

    
147
    @Override
148
    public Statement createStatement() throws SQLException {
149
        return this.conn.createStatement();
150
    }
151

    
152
    @Override
153
    public Statement createStatement(String sql) throws SQLException {
154
        return this.conn.createStatement();
155
    }
156

    
157
    @Override
158
    public PreparedStatement prepareStatement(String sql) throws SQLException {
159
        return this.conn.prepareStatement(sql);
160
    }
161

    
162
    @Override
163
    public DatabaseMetaData getMetaData() throws SQLException {
164
        return this.conn.getMetaData();
165
    }
166
    
167
    @Override
168
    public void dispose() {
169
        try {
170
            try {
171
                this.conn.rollback();
172
            } catch(Exception ex) {
173
                LOGGER.debug("",ex);
174
            }
175
            String connid = JDBCUtils.getConnId(this.conn);
176
            this.conn.close();        
177
            if( LOGGER.isDebugEnabled() ) {
178
                if( this.connectionProvider==null ) {
179
                    LOGGER.debug("CONN CLOSE "+connid+" trans: NULL status: UNKNOWN");
180
                } else {
181
                    LOGGER.debug("CONN CLOSE "+connid+" trans: NULL status: "+this.connectionProvider.getStatus());
182
                }
183
            }
184
            this.conn = null;
185
            this.transaction = null;
186
        } catch(Exception ex) {
187
            LOGGER.debug("",ex);
188
        }
189
    }
190
    
191
    @Override
192
    public void finish() throws DataException {
193
        try {
194
            this.conn.commit();
195
            this.conn.setAutoCommit(true);
196
        } catch (SQLException ex) {
197
            throw new DataSQLException(ex);
198
        }
199
    }
200

    
201
    @Override
202
    public void abort() throws DataException {
203
        try {
204
            this.conn.rollback();
205
            this.conn.setAutoCommit(true);
206
        } catch (SQLException ex) {
207
            throw new DataSQLException(ex);
208
        }
209
    }
210

    
211
}