Revision 43377 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
AbstractConnectionOperation.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.sql.Connection; |
4 | 4 |
import java.sql.SQLException; |
5 |
import java.util.logging.Level; |
|
6 |
import org.gvsig.fmap.dal.exception.DataException; |
|
7 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
|
8 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
|
5 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
9 | 6 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException; |
10 | 7 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
11 | 8 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
... | ... | |
18 | 15 |
|
19 | 16 |
final protected JDBCHelper helper; |
20 | 17 |
|
21 |
private Connection conn = null;
|
|
18 |
protected Connection conn = null;
|
|
22 | 19 |
|
23 | 20 |
public AbstractConnectionOperation(JDBCHelper helper) { |
24 | 21 |
this.helper = helper; |
... | ... | |
49 | 46 |
} |
50 | 47 |
} |
51 | 48 |
|
52 |
protected Connection getConnection() { |
|
49 |
protected Connection getConnection() throws AccessResourceException { |
|
50 |
if( this.conn == null ) { |
|
51 |
this.conn = this.helper.getConnection(); |
|
52 |
} |
|
53 | 53 |
return this.conn; |
54 | 54 |
} |
55 | 55 |
|
56 |
public Object perform_operation() throws Exception {
|
|
57 |
Object result = null;
|
|
58 |
try {
|
|
59 |
logger.debug("preparing execution of "+this.getClass().getSimpleName()+".");
|
|
60 |
conn = helper.getConnection();
|
|
61 |
|
|
62 |
// XXX OJO esta condicion NO ES FIABLE
|
|
63 |
if (!conn.getAutoCommit()) {
|
|
64 |
if (!continueTransactionAllowed()) {
|
|
65 |
throw new SQLException("nested operations not allowed.");
|
|
56 |
protected Object perform_operation() throws Exception {
|
|
57 |
Object result = null; |
|
58 |
logger.debug("preparing execution of "+this.getClass().getSimpleName()+".");
|
|
59 |
if( needTransaction() ) {
|
|
60 |
try {
|
|
61 |
// XXX OJO esta condicion NO ES FIABLE |
|
62 |
if (!this.getConnection().getAutoCommit()) {
|
|
63 |
if (!continueTransactionAllowed()) {
|
|
64 |
throw new SQLException("nested operations not allowed.");
|
|
65 |
}
|
|
66 | 66 |
} |
67 |
this.getConnection().setAutoCommit(false); |
|
68 |
try { |
|
69 |
logger.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
|
70 |
result = perform(this.getConnection()); |
|
71 |
} catch (Exception ex) { |
|
72 |
try { |
|
73 |
this.getConnection().rollback(); |
|
74 |
} catch (Exception e1) { |
|
75 |
throw new JDBCTransactionRollbackException(e1, ex); |
|
76 |
} |
|
77 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
|
78 |
} |
|
79 |
this.getConnection().commit(); |
|
80 |
} finally { |
|
81 |
helper.closeConnection(this.getConnection()); |
|
82 |
conn = null; |
|
67 | 83 |
} |
68 |
conn.setAutoCommit(false);
|
|
84 |
} else {
|
|
69 | 85 |
try { |
70 | 86 |
logger.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
71 |
result = perform(conn);
|
|
87 |
result = perform(this.getConnection());
|
|
72 | 88 |
} catch (Exception ex) { |
73 |
try { |
|
74 |
conn.rollback(); |
|
75 |
} catch (Exception e1) { |
|
76 |
throw new JDBCTransactionRollbackException(e1, ex); |
|
77 |
} |
|
78 | 89 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
90 |
} finally { |
|
91 |
helper.closeConnection(this.getConnection()); |
|
92 |
conn = null; |
|
79 | 93 |
} |
80 |
conn.commit(); |
|
81 |
return result; |
|
82 |
} finally { |
|
83 |
helper.closeConnection(conn); |
|
84 |
conn = null; |
|
85 | 94 |
} |
95 |
return result; |
|
86 | 96 |
} |
87 |
// |
|
88 |
// public Object perform2() { |
|
89 |
// ResourceProvider resource = this.helper.getResource(); |
|
90 |
// Object r = resource.execute(new ResourceAction() { |
|
91 |
// |
|
92 |
// @Override |
|
93 |
// public Object run() throws Exception { |
|
94 |
// return perform_operation(); |
|
95 |
// } |
|
96 |
// }); |
|
97 |
// return r; |
|
98 |
// } |
|
99 |
|
|
100 | 97 |
} |
Also available in: Unified diff