1616import java .util .concurrent .locks .ReentrantLock ;
1717
1818/**
19- * The main database class.
19+ * The main database class used by the application.
20+ * <p>
21+ * Create an instance using {@link #Database(String)} and prefer to re-use it. The underlying
22+ * connections are handled automatically by the system.
23+ * <p>
24+ * Instances of this class are thread-safe and can be used to concurrently write to the database.
2025 */
2126public final class Database {
2227
28+ /**
29+ * The DSL context for this database.
30+ */
2331 private final DSLContext dslContext ;
32+ /**
33+ * Lock used to implement thread-safety across this class. Any database modifying method should
34+ * use this lock.
35+ */
2436 private final Lock writeLock = new ReentrantLock ();
2537
2638 /**
27- * Creates a new database.
39+ * Creates an instance of a new database.
2840 *
29- * @param jdbcUrl the url to the database
41+ * @param jdbcUrl the url to the database in the format expected by JDBC
3042 * @throws SQLException if no connection could be established
3143 */
3244 public Database (String jdbcUrl ) throws SQLException {
3345 SQLiteConfig sqliteConfig = new SQLiteConfig ();
3446 sqliteConfig .enforceForeignKeys (true );
35- // In WAL mode only concurrent writes pose a problem so we synchronize on those
47+ // In WAL mode only concurrent writes pose a problem, so we synchronize on those
3648 sqliteConfig .setJournalMode (SQLiteConfig .JournalMode .WAL );
3749
3850 SQLiteDataSource dataSource = new SQLiteDataSource (sqliteConfig );
@@ -45,10 +57,12 @@ public Database(String jdbcUrl) throws SQLException {
4557 }
4658
4759 /**
48- * Acquires read access to the database.
60+ * Acquires read-only access to the database.
4961 *
50- * @return a way to interact with the database in a read only way
51- * @throws DatabaseException if an error occurs in the passed handler function
62+ * @param action the action to apply to the DSL context, e.g. a query
63+ * @param <T> the type returned by the given action
64+ * @return the result returned by the given action
65+ * @throws DatabaseException if an error occurs in the given action
5266 */
5367 public <T > T read (
5468 CheckedFunction <? super DSLContext , T , ? extends DataAccessException > action ) {
@@ -60,9 +74,10 @@ public <T> T read(
6074 }
6175
6276 /**
63- * Acquires read access to the database.
77+ * Acquires read-only access to the database.
6478 *
65- * @throws DatabaseException if an error occurs in the passed handler function
79+ * @param action the action that consumes the DSL context, e.g. a query
80+ * @throws DatabaseException if an error occurs in the given action
6681 */
6782 public void read (CheckedConsumer <? super DSLContext , ? extends DataAccessException > action ) {
6883 read (context -> {
@@ -74,8 +89,10 @@ public void read(CheckedConsumer<? super DSLContext, ? extends DataAccessExcepti
7489 /**
7590 * Acquires read and write access to the database.
7691 *
77- * @return a way to interact with the database
78- * @throws DatabaseException if an error occurs in the passed handler function
92+ * @param action the action to apply to the DSL context, e.g. a query
93+ * @param <T> the type returned by the given action
94+ * @return the result returned by the given action
95+ * @throws DatabaseException if an error occurs in the given action
7996 */
8097 public <T > T write (
8198 CheckedFunction <? super DSLContext , T , ? extends DataAccessException > action ) {
@@ -92,7 +109,8 @@ public <T> T write(
92109 /**
93110 * Acquires read and write access to the database.
94111 *
95- * @throws DatabaseException if an error occurs in the passed handler function
112+ * @param action the action to apply to the DSL context, e.g. a query
113+ * @throws DatabaseException if an error occurs in the given action
96114 */
97115 public void write (CheckedConsumer <? super DSLContext , ? extends DataAccessException > action ) {
98116 write (context -> {
@@ -107,8 +125,8 @@ public void write(CheckedConsumer<? super DSLContext, ? extends DataAccessExcept
107125 * @param handler the handler that is executed within the context of the transaction. The
108126 * handler will be called once and its return value returned from the transaction.
109127 * @param <T> the handler's return type
110- * @return whatever the handler returned
111- * @throws DatabaseException if an error occurs in the passed handler function
128+ * @return the object that is returned by the given handler
129+ * @throws DatabaseException if an error occurs in the given handler function
112130 */
113131 public <T > T readTransaction (
114132 CheckedFunction <? super DSLContext , T , DataAccessException > handler ) {
@@ -128,7 +146,7 @@ public <T> T readTransaction(
128146 *
129147 * @param handler the handler that is executed within the context of the transaction. It has no
130148 * return value.
131- * @throws DatabaseException if an error occurs in the passed handler function
149+ * @throws DatabaseException if an error occurs in the given handler function
132150 */
133151 public void readTransaction (
134152 CheckedConsumer <? super DSLContext , ? extends DataAccessException > handler ) {
@@ -142,10 +160,10 @@ public void readTransaction(
142160 * Acquires a transaction that can read and write to the database.
143161 *
144162 * @param handler the handler that is executed within the context of the transaction. The
145- * handler will be called once and its return value returned from the transaction.
146- * @param <T> the handler's return type
147- * @return whatever the handler returned
148- * @throws DatabaseException if an error occurs in the passed handler function
163+ * handler will be called once and its return value is returned from the transaction.
164+ * @param <T> the return type of the handler
165+ * @return the object that is returned by the given handler
166+ * @throws DatabaseException if an error occurs in the given handler function
149167 */
150168 public <T > T writeTransaction (
151169 CheckedFunction <? super DSLContext , T , DataAccessException > handler ) {
@@ -168,7 +186,7 @@ public <T> T writeTransaction(
168186 *
169187 * @param handler the handler that is executed within the context of the transaction. It has no
170188 * return value.
171- * @throws DatabaseException if an error occurs in the passed handler function
189+ * @throws DatabaseException if an error occurs in the given handler function
172190 */
173191 public void writeTransaction (
174192 CheckedConsumer <? super DSLContext , ? extends DataAccessException > handler ) {
@@ -179,7 +197,9 @@ public void writeTransaction(
179197 }
180198
181199 /**
182- * @return the database dsl context
200+ * Gets the DSL context for this database.
201+ *
202+ * @return the DSL context
183203 */
184204 private DSLContext getDslContext () {
185205 return dslContext ;
0 commit comments