Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/db/DbConnection.java


1   /*
2       Javatools (modified version) - Some useful general classes.
3       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      Contact me at: brenmcguire@users.sourceforge.net
20   */
21  package javatools.db;
22  import java.sql.*;
23  import javatools.util.FileLog;
24  /**
25   * A Database connection.
26   *
27   * @author Chris Bitmead
28   * @created December 13, 2001
29   * @version 0.2.2
30   * @commentby Antonio Petrelli
31   */
32  
33  public class DbConnection {
34      /** The database to connect to.
35       */    
36    DbDatabase database;
37          /** The real Connection object.
38           */        
39    Connection con;
40          /** It should contain the current thread, or a new thread, in case a specific
41           * function in DbDatabase is called.
42           */        
43    Thread createdInThread;
44  
45          /** Creates a new DbConnection.
46           * @param database The database to connect to.
47           * @param con The real JDBC connection.
48           */        
49    public DbConnection(DbDatabase database, Connection con) {
50      this.database = database;
51      this.con = con;
52      this.createdInThread = Thread.currentThread();
53    }
54  
55          /** Returns the real JDBC connection. It is useful to send commands directly to
56           * JDBC.
57           * @return The needed connection.
58           */        
59    public Connection getSqlConnection() {
60      return con;
61    }
62  
63          /** Checks if this connection is closed.
64           * @throws DbException If something goes wrong.
65           * @return <CODE>true</CODE>: if this connection is closed;
66           * <CODE>false</CODE>: if it is open.
67           */        
68    public boolean isClosed() throws DbException {
69      try {
70        boolean rtn = (con == null || con.isClosed());
71        // Extra hope of cleanup.
72        if (rtn) {
73          close();
74        }
75        return rtn;
76      } catch (SQLException e) {
77        throw new DbException(e);
78      }
79    }
80  
81          /** Commits all the operations and closes the connection.
82           * @throws DbException If something goes wrong.
83           */        
84    public void commitClose() throws DbException {
85      commit();
86      close();
87    }
88  
89          /** Rollbacks operations and closes the connection.
90           * @throws DbException If something goes wrong.
91           */        
92    public void rollbackClose() throws DbException {
93      rollback();
94      close();
95    }
96  
97          /** Commits all operations.
98           * @throws DbException If something goes wrong.
99           */        
100   public void commit() throws DbException {
101     try {
102       con.commit();
103     } catch (SQLException e) {
104       throw new DbException(e);
105     }
106   }
107 
108         /** Rollbacks operations.
109          * @throws DbException If something goes wrong.
110          */        
111   public void rollback() throws DbException {
112     try {
113       con.rollback();
114     } catch (SQLException e) {
115       throw new DbException(e);
116     }
117   }
118 
119         /** Closes the connection.
120          * @throws DbException If something goes wrong.
121          */        
122   public void close() throws DbException {
123     FileLog.singleton().debug("Connection.close", "for db: " + database + " in thread: " + Thread.currentThread().getName() + " Created in thread: " + createdInThread.getName());
124     try {
125       if (con != null && !con.isClosed()) {
126         con.close();
127         con = null;
128       }
129       database.notifyClose(this);
130     } catch (SQLException e) {
131       throw new DbException(e);
132     }
133   }
134         
135         public void setAutoCommit(boolean value) throws DbException {
136                 try {
137                         con.setAutoCommit(value);
138                 }
139                 catch (SQLException e) {
140                         throw new DbException(e);
141                 }
142         }
143 
144         /** Destroys this object.
145          * @throws Throwable If something goes wrong.
146          */        
147   protected void finalize() throws java.lang.Throwable {
148     close();
149   }
150 }