01: /*
02: * $Id: JdbcTransaction.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transport.jdbc;
12:
13: import org.mule.api.transaction.TransactionException;
14: import org.mule.config.i18n.CoreMessages;
15: import org.mule.transaction.AbstractSingleResourceTransaction;
16: import org.mule.transaction.IllegalTransactionStateException;
17: import org.mule.transaction.TransactionRollbackException;
18: import org.mule.transport.jdbc.i18n.JdbcMessages;
19:
20: import java.sql.Connection;
21: import java.sql.SQLException;
22:
23: import javax.sql.DataSource;
24:
25: /**
26: * TODO
27: */
28: public class JdbcTransaction extends AbstractSingleResourceTransaction {
29:
30: public JdbcTransaction() {
31: super ();
32: }
33:
34: public void bindResource(Object key, Object resource)
35: throws TransactionException {
36: if (!(key instanceof DataSource)
37: || !(resource instanceof Connection)) {
38: throw new IllegalTransactionStateException(
39: CoreMessages
40: .transactionCanOnlyBindToResources("javax.sql.DataSource/java.sql.Connection"));
41: }
42: Connection con = (Connection) resource;
43: try {
44: if (con.getAutoCommit()) {
45: con.setAutoCommit(false);
46: }
47: } catch (SQLException e) {
48: throw new TransactionException(JdbcMessages
49: .transactionSetAutoCommitFailed(), e);
50: }
51: super .bindResource(key, resource);
52: }
53:
54: protected void doBegin() throws TransactionException {
55: // Do nothing
56: }
57:
58: protected void doCommit() throws TransactionException {
59: try {
60: ((Connection) resource).commit();
61: ((Connection) resource).close();
62: } catch (SQLException e) {
63: throw new TransactionException(CoreMessages
64: .transactionCommitFailed(), e);
65: }
66: }
67:
68: protected void doRollback() throws TransactionException {
69: try {
70: ((Connection) resource).rollback();
71: ((Connection) resource).close();
72: } catch (SQLException e) {
73: throw new TransactionRollbackException(CoreMessages
74: .transactionRollbackFailed(), e);
75: }
76: }
77: }
|