1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 }