View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.database;
29  
30  import java.sql.Connection;
31  import java.sql.SQLException;
32  
33  import javax.sql.DataSource;
34  
35  /***
36   * A generic implementaion of the {@Database} interface.
37   * 
38   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
39   * @version $Id: DefaultDatabase.java,v 1.2 2005/04/04 11:38:08 rafal Exp $
40   */
41  public class DefaultDatabase implements Database
42  {
43      /*** the source of database connections. */
44      private DataSource dataSource;
45      
46      /*** the generator of per-table unique ids. */
47      private IdGenerator idGenerator;
48      
49      /*** the global transaction management helper object. */
50      private Transaction transaction;
51      
52      /***
53       * Creates a Database instance.
54       * 
55       * @param dataSource the source of database connections. 
56       * @param idGenerator the generator of per-table unique ids.
57       * @param transaction the global transaction management helper object.
58       */
59      public DefaultDatabase(DataSource dataSource, IdGenerator idGenerator, Transaction transaction)
60      {
61          this.dataSource = dataSource;
62          this.idGenerator = idGenerator;
63          this.transaction = transaction;
64      }
65  
66      /***
67       * {@inheritDoc}
68       */ 
69      public Connection getConnection()
70          throws SQLException
71      {
72          return dataSource.getConnection();
73      }
74      
75      /***
76       * {@inheritDoc}
77       */ 
78      public Connection getConnection(String user, String password)
79          throws SQLException
80      {
81          return dataSource.getConnection(user, password);
82      }
83  
84      /***
85       * {@inheritDoc}
86       */ 
87      public long getNextId(String table)
88          throws SQLException
89      {
90          return idGenerator.getNextId(table);
91      }
92      
93      /***
94       * {@inheritDoc}
95       */
96      public void setTransactionTimeout(int seconds)
97          throws SQLException
98      {
99          transaction.setTransactionTimeout(seconds);
100     }
101 
102     /***
103      * {@inheritDoc}
104      */ 
105     public boolean beginTransaction()
106         throws SQLException
107     {
108         return transaction.begin();
109     }
110     
111     /***
112      * {@inheritDoc}
113      */ 
114     public void commitTransaction(boolean controller)
115         throws SQLException
116     {
117         transaction.commit(controller);
118     }
119     
120     /***
121      * {@inheritDoc}
122      */ 
123     public void rollbackTransaction(boolean controller)
124         throws SQLException
125     {
126         transaction.rollback(controller);
127     }
128 }