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 / operations / AbstractConnectionOperation.java @ 44198
History | View | Annotate | Download (5 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
---|---|---|---|
2 | |||
3 | import java.sql.Connection; |
||
4 | import java.sql.SQLException; |
||
5 | 43377 | jjdelcerro | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
6 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException; |
7 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
8 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
9 | import org.slf4j.Logger; |
||
10 | import org.slf4j.LoggerFactory; |
||
11 | |||
12 | 44191 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
13 | 43020 | jjdelcerro | public abstract class AbstractConnectionOperation implements ConnectionOperation { |
14 | |||
15 | 44058 | jjdelcerro | final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class); |
16 | 43020 | jjdelcerro | |
17 | final protected JDBCHelper helper; |
||
18 | 43114 | jjdelcerro | |
19 | 43377 | jjdelcerro | protected Connection conn = null; |
20 | 43020 | jjdelcerro | |
21 | public AbstractConnectionOperation(JDBCHelper helper) {
|
||
22 | this.helper = helper;
|
||
23 | } |
||
24 | |||
25 | @Override
|
||
26 | public boolean continueTransactionAllowed() { |
||
27 | return false; |
||
28 | } |
||
29 | |||
30 | @Override
|
||
31 | public boolean needTransaction() { |
||
32 | return true; |
||
33 | } |
||
34 | |||
35 | protected JDBCSQLBuilderBase createSQLBuilder() {
|
||
36 | return this.helper.createSQLBuilder(); |
||
37 | } |
||
38 | |||
39 | @Override
|
||
40 | public Object perform() { |
||
41 | try {
|
||
42 | 44198 | jjdelcerro | if( this.helper.isThreadSafe() ) { |
43 | return this.perform_operation(); |
||
44 | } |
||
45 | synchronized(AbstractConnectionOperation.class) {
|
||
46 | return this.perform_operation(); |
||
47 | } |
||
48 | 43020 | jjdelcerro | } catch (RuntimeException ex) { |
49 | throw ex;
|
||
50 | } catch (Exception ex) { |
||
51 | throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | 43377 | jjdelcerro | protected Connection getConnection() throws AccessResourceException { |
56 | if( this.conn == null ) { |
||
57 | this.conn = this.helper.getConnection(); |
||
58 | } |
||
59 | 43114 | jjdelcerro | return this.conn; |
60 | } |
||
61 | |||
62 | 43377 | jjdelcerro | protected Object perform_operation() throws Exception { |
63 | Object result = null; |
||
64 | 44058 | jjdelcerro | LOGGER.debug("preparing execution of "+this.getClass().getSimpleName()+"."); |
65 | 43377 | jjdelcerro | if( needTransaction() ) {
|
66 | try {
|
||
67 | 44191 | jjdelcerro | if( this.helper.allowNestedOperations() ) { |
68 | boolean commitOnFinish = true; |
||
69 | if (!this.getConnection().getAutoCommit()) { |
||
70 | // Casi seguro que es una operacion anidada, asi que no
|
||
71 | // terminamos la transaccion.
|
||
72 | commitOnFinish = false;
|
||
73 | } |
||
74 | this.getConnection().setAutoCommit(false); |
||
75 | try {
|
||
76 | LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
||
77 | result = perform(this.getConnection());
|
||
78 | } catch (Exception ex) { |
||
79 | try {
|
||
80 | this.getConnection().rollback();
|
||
81 | } catch (Exception e1) { |
||
82 | throw new JDBCTransactionRollbackException(e1, ex); |
||
83 | } |
||
84 | throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
||
85 | 43377 | jjdelcerro | } |
86 | 44191 | jjdelcerro | if( commitOnFinish ) {
|
87 | this.getConnection().commit();
|
||
88 | this.getConnection().setAutoCommit(true); |
||
89 | } |
||
90 | } else {
|
||
91 | if (!this.getConnection().getAutoCommit()) { |
||
92 | // Casi seguro que es una transaccion anidada, si no
|
||
93 | // esta permitido, petamos.
|
||
94 | if (!continueTransactionAllowed()) {
|
||
95 | throw new SQLException("nested operations not allowed."); |
||
96 | } |
||
97 | } |
||
98 | this.getConnection().setAutoCommit(false); |
||
99 | 43377 | jjdelcerro | try {
|
100 | 44191 | jjdelcerro | LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
101 | result = perform(this.getConnection());
|
||
102 | } catch (Exception ex) { |
||
103 | try {
|
||
104 | this.getConnection().rollback();
|
||
105 | } catch (Exception e1) { |
||
106 | throw new JDBCTransactionRollbackException(e1, ex); |
||
107 | } |
||
108 | throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
||
109 | 43377 | jjdelcerro | } |
110 | 44191 | jjdelcerro | this.getConnection().commit();
|
111 | 43377 | jjdelcerro | } |
112 | } finally {
|
||
113 | helper.closeConnection(this.getConnection());
|
||
114 | conn = null;
|
||
115 | 43020 | jjdelcerro | } |
116 | 43377 | jjdelcerro | } else {
|
117 | 43020 | jjdelcerro | try {
|
118 | 44058 | jjdelcerro | LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
119 | 43377 | jjdelcerro | result = perform(this.getConnection());
|
120 | 43020 | jjdelcerro | } catch (Exception ex) { |
121 | throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
||
122 | 43377 | jjdelcerro | } finally {
|
123 | helper.closeConnection(this.getConnection());
|
||
124 | conn = null;
|
||
125 | 43020 | jjdelcerro | } |
126 | } |
||
127 | 43377 | jjdelcerro | return result;
|
128 | 43020 | jjdelcerro | } |
129 | } |