View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.jdbc;
22  
23  import org.slf4j.*;
24  import org.synchronoss.cpo.*;
25  import org.synchronoss.cpo.jdbc.meta.JdbcCpoMetaDescriptor;
26  
27  import java.sql.*;
28  import java.util.HashMap;
29  
30  public class JdbcCpoTrxAdapter extends JdbcCpoAdapter implements CpoTrxAdapter {
31  
32    /**
33     * Version Id for this class.
34     */
35    private static final long serialVersionUID = 1L;
36  
37    private static final Logger logger = LoggerFactory.getLogger(JdbcCpoAdapter.class);
38  
39    /**
40     * DOCUMENT ME!
41     */
42    // Default Connection. Only used JdbcCpoTrxAdapter
43    private Connection writeConnection_ = null;
44    // map to keep track of busy connections
45    private static final HashMap<Connection, Connection> busyMap_ = new HashMap<Connection, Connection>();
46  
47    @SuppressWarnings("unused")
48    private JdbcCpoTrxAdapter() {
49    }
50  
51    protected JdbcCpoTrxAdapter(JdbcCpoMetaDescriptor metaDescriptor, Connection c,
52            boolean batchSupported, String dataSourceName) throws CpoException {
53      super(metaDescriptor, batchSupported, dataSourceName);
54      setStaticConnection(c);
55    }
56  
57    @Override
58    public void commit() throws CpoException {
59      if (writeConnection_ != null) {
60        try {
61          writeConnection_.commit();
62        } catch (SQLException se) {
63          throw new CpoException(se.getMessage());
64        }
65      } else {
66        throw new CpoException("Transaction Object has been Closed");
67      }
68    }
69  
70    @Override
71    public void rollback() throws CpoException {
72      if (writeConnection_ != null) {
73        try {
74          writeConnection_.rollback();
75        } catch (Exception e) {
76          throw new CpoException(e.getMessage());
77        }
78      } else {
79        throw new CpoException("Transaction Object has been Closed");
80      }
81    }
82  
83    @Override
84    public boolean isClosed() throws CpoException {
85      boolean closed = false;
86  
87      try {
88        closed = (writeConnection_ == null || writeConnection_.isClosed());
89      } catch (Exception e) {
90        throw new CpoException(e.getMessage());
91      }
92      return closed;
93    }
94  
95    @Override
96    public void close() throws CpoException {
97      if (writeConnection_ != null) {
98        try {
99          try {
100           writeConnection_.rollback();
101         } catch (Exception e) {
102           if (logger.isTraceEnabled()) {
103             logger.trace(e.getLocalizedMessage());
104           }
105         }
106         try {
107           writeConnection_.close();
108         } catch (Exception e) {
109           if (logger.isTraceEnabled()) {
110             logger.trace(e.getLocalizedMessage());
111           }
112         }
113       } finally {
114         setStaticConnection(null);
115       }
116     }
117   }
118 
119   /**
120    * DOCUMENT ME!
121    */
122   @Override
123   protected void finalize() {
124     try {
125       super.finalize();
126     } catch (Throwable e) {
127       if (logger.isTraceEnabled()) {
128         logger.trace(e.getLocalizedMessage());
129       }
130     }
131     try {
132       if (writeConnection_ != null && !writeConnection_.isClosed()) {
133         this.close();
134       }
135     } catch (Exception e) {
136       if (logger.isTraceEnabled()) {
137         logger.trace(e.getLocalizedMessage());
138       }
139     }
140   }
141 
142   @Override
143   protected Connection getStaticConnection() throws CpoException {
144     if (writeConnection_ != null) {
145       if (isConnectionBusy(writeConnection_)) {
146         throw new CpoException("Error Connection Busy");
147       } else {
148         setConnectionBusy(writeConnection_);
149       }
150     }
151     return writeConnection_;
152   }
153 
154   @Override
155   protected boolean isStaticConnection(Connection c) {
156     return (writeConnection_ == c);
157   }
158 
159   @Override
160   protected void setStaticConnection(Connection c) {
161     writeConnection_ = c;
162   }
163 
164   @Override
165   protected boolean isConnectionBusy(Connection c) {
166     synchronized (busyMap_) {
167       Connection test = busyMap_.get(c);
168       return test != null;
169     }
170   }
171 
172   @Override
173   protected void setConnectionBusy(Connection c) {
174     synchronized (busyMap_) {
175       busyMap_.put(c, c);
176     }
177   }
178 
179   @Override
180   protected void clearConnectionBusy(Connection c) {
181     synchronized (busyMap_) {
182       busyMap_.remove(c);
183     }
184   }
185 }