Skip to content

Commit ddefab0

Browse files
committed
Pkgenerator methods should not throw checked exceptions
1 parent c1ac549 commit ddefab0

7 files changed

Lines changed: 38 additions & 26 deletions

File tree

cayenne/src/main/java/org/apache/cayenne/dba/JdbcPkGenerator.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public JdbcAdapter getAdapter() {
7575
return this.adapter;
7676
}
7777

78-
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
78+
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) {
7979
// check if a table exists
8080

8181
// create AUTO_PK_SUPPORT table
@@ -110,7 +110,7 @@ public List<String> createAutoPkStatements(List<DbEntity> dbEntities) {
110110
/**
111111
* Drops table named "AUTO_PK_SUPPORT" if it exists in the database.
112112
*/
113-
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
113+
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) {
114114
if (autoPkTableExists(node)) {
115115
runUpdate(node, dropAutoPkString());
116116
}
@@ -161,34 +161,36 @@ protected String dropAutoPkString() {
161161
/**
162162
* Checks if AUTO_PK_TABLE already exists in the database.
163163
*/
164-
protected boolean autoPkTableExists(DataNode node) throws SQLException {
164+
protected boolean autoPkTableExists(DataNode node) {
165165

166166
try (Connection con = node.getDataSource().getConnection()) {
167167
DatabaseMetaData md = con.getMetaData();
168168
try (ResultSet tables = md.getTables(null, null, "AUTO_PK_SUPPORT", null)) {
169169
return tables.next();
170170
}
171+
} catch (SQLException e) {
172+
throw new CayenneRuntimeException("Error checking for AUTO_PK_SUPPORT table", e);
171173
}
172174
}
173175

174176
/**
175177
* Runs JDBC update over a Connection obtained from DataNode. Returns a
176178
* number of objects returned from update.
177-
*
178-
* @throws SQLException in case of query failure.
179179
*/
180-
public int runUpdate(DataNode node, String sql) throws SQLException {
180+
public int runUpdate(DataNode node, String sql) {
181181
adapter.getJdbcEventLogger().log(sql);
182182

183183
try (Connection con = node.getDataSource().getConnection()) {
184184
try (Statement upd = con.createStatement()) {
185185
return upd.executeUpdate(sql);
186186
}
187+
} catch (SQLException e) {
188+
throw new CayenneRuntimeException("Error executing PK update: %s", e, sql);
187189
}
188190
}
189191

190192
@Override
191-
public Object generatePk(DataNode node, DbAttribute pk, Class<?> javaType) throws Exception {
193+
public Object generatePk(DataNode node, DbAttribute pk, Class<?> javaType) {
192194

193195
DbEntity entity = pk.getEntity();
194196

@@ -268,7 +270,7 @@ public void setAdapter(DbAdapter adapter) {
268270
*
269271
* @since 3.0
270272
*/
271-
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
273+
protected long longPkFromDatabase(DataNode node, DbEntity entity) {
272274
String select = "SELECT #result('NEXT_ID' 'long' 'NEXT_ID') FROM AUTO_PK_SUPPORT "
273275
+ "WHERE TABLE_NAME = '" + entity.getName() + '\'';
274276

cayenne/src/main/java/org/apache/cayenne/dba/PkGenerator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public interface PkGenerator {
3838
* @param dbEntities a list of entities that require primary key auto-generation
3939
* support
4040
*/
41-
void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception;
41+
void createAutoPk(DataNode node, List<DbEntity> dbEntities);
4242

4343
/**
4444
* Returns a list of SQL strings needed to generates database objects to provide
@@ -55,7 +55,7 @@ public interface PkGenerator {
5555
* @param dbEntities a list of entities whose primary key auto-generation support
5656
* should be dropped.
5757
*/
58-
void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception;
58+
void dropAutoPk(DataNode node, List<DbEntity> dbEntities);
5959

6060
/**
6161
* Returns SQL string needed to drop database objects associated with automatic
@@ -69,7 +69,7 @@ public interface PkGenerator {
6969
* @deprecated unused by the framework that relies on {@link #generatePk(DataNode, DbAttribute, Class)}
7070
*/
7171
@Deprecated(since = "5.0", forRemoval = true)
72-
default Object generatePk(DataNode dataNode, DbAttribute pk) throws Exception {
72+
default Object generatePk(DataNode dataNode, DbAttribute pk) {
7373
return generatePk(dataNode, pk, null);
7474
}
7575

@@ -78,7 +78,7 @@ default Object generatePk(DataNode dataNode, DbAttribute pk) throws Exception {
7878
*
7979
* @since 5.0
8080
*/
81-
Object generatePk(DataNode dataNode, DbAttribute pk, Class<?> javaType) throws Exception;
81+
Object generatePk(DataNode dataNode, DbAttribute pk, Class<?> javaType);
8282

8383
/**
8484
* Install the adapter associated with current PkGenerator

cayenne/src/main/java/org/apache/cayenne/dba/frontbase/FrontBasePkGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public int getPkCacheSize() {
6161
}
6262

6363
@Override
64-
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
64+
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) {
6565
// For each entity (re)set the unique counter
6666
for (DbEntity entity : dbEntities) {
6767
runUpdate(node, pkCreateString(entity.getName()));
@@ -78,7 +78,7 @@ public List<String> createAutoPkStatements(List<DbEntity> dbEntities) {
7878
}
7979

8080
@Override
81-
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
81+
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) {
8282
}
8383

8484
@Override
@@ -119,7 +119,7 @@ protected String dropAutoPkString() {
119119
* @since 3.0
120120
*/
121121
@Override
122-
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
122+
protected long longPkFromDatabase(DataNode node, DbEntity entity) {
123123

124124
String template = "SELECT #result('UNIQUE' 'long') FROM " + entity.getName();
125125

cayenne/src/main/java/org/apache/cayenne/dba/mysql/MySQLPkGenerator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.sql.Statement;
2626
import java.util.Collections;
2727

28+
import org.apache.cayenne.CayenneRuntimeException;
2829
import org.apache.cayenne.access.DataNode;
2930
import org.apache.cayenne.dba.JdbcAdapter;
3031
import org.apache.cayenne.dba.JdbcPkGenerator;
@@ -59,7 +60,7 @@ public MySQLPkGenerator() {
5960
* @since 3.0
6061
*/
6162
@Override
62-
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
63+
protected long longPkFromDatabase(DataNode node, DbEntity entity) {
6364

6465
// must work directly with JDBC connection, since we
6566
// must unlock the AUTO_PK_SUPPORT table in case of
@@ -115,7 +116,7 @@ protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Excepti
115116

116117
// check errors
117118
if (exception != null) {
118-
throw exception;
119+
throw new CayenneRuntimeException("Error generating pk for DbEntity %s", exception, entity.getName());
119120
}
120121

121122
return pk;

cayenne/src/main/java/org/apache/cayenne/dba/oracle/OraclePkGenerator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected OraclePkGenerator(JdbcAdapter adapter) {
6767
private static final String _SEQUENCE_PREFIX = "pk_";
6868

6969
@Override
70-
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
70+
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) {
7171
List<String> sequences = getExistingSequences(node);
7272
// create needed sequences
7373
for (DbEntity dbEntity : dbEntities) {
@@ -94,7 +94,7 @@ public List<String> createAutoPkStatements(List<DbEntity> dbEntities) {
9494
* Drops PK sequences for all specified DbEntities.
9595
*/
9696
@Override
97-
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
97+
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) {
9898
List<String> sequences = getExistingSequences(node);
9999

100100
// drop obsolete sequences
@@ -160,7 +160,7 @@ protected String selectAllSequencesQuery() {
160160
* @since 3.0
161161
*/
162162
@Override
163-
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
163+
protected long longPkFromDatabase(DataNode node, DbEntity entity) {
164164

165165
DbKeyGenerator pkGenerator = entity.getPrimaryKeyGenerator();
166166
String pkGeneratingSequenceName;
@@ -183,6 +183,8 @@ protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Excepti
183183
return rs.getLong(1);
184184
}
185185
}
186+
} catch (SQLException e) {
187+
throw new CayenneRuntimeException("Error generating pk for DbEntity %s", e, entity.getName());
186188
}
187189
}
188190

@@ -229,7 +231,7 @@ private String stripSchemaName(String sequenceName) {
229231
* Fetches a list of existing sequences that might match Cayenne generated
230232
* ones.
231233
*/
232-
protected List<String> getExistingSequences(DataNode node) throws SQLException {
234+
protected List<String> getExistingSequences(DataNode node) {
233235

234236
// check existing sequences
235237
try (Connection con = node.getDataSource().getConnection()) {
@@ -248,6 +250,8 @@ protected List<String> getExistingSequences(DataNode node) throws SQLException {
248250
return sequenceList;
249251
}
250252
}
253+
} catch (SQLException e) {
254+
throw new CayenneRuntimeException("Error fetching existing sequences", e);
251255
}
252256
}
253257
}

cayenne/src/main/java/org/apache/cayenne/dba/sqlserver/SQLServerPkGenerator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public List<String> dropAutoPkStatements(List<DbEntity> dbEntities) {
105105
}
106106

107107
@Override
108-
public Object generatePk(DataNode node, DbAttribute pk, Class<?> javaType) throws Exception {
108+
public Object generatePk(DataNode node, DbAttribute pk, Class<?> javaType) {
109109
DbEntity entity = pk.getEntity();
110110

111111
//check key on UNIQUEIDENTIFIER; UNIQUEIDENTIFIER is a character with a length of 36
@@ -139,7 +139,7 @@ protected String sequenceName(DbEntity entity) {
139139
}
140140
}
141141

142-
protected String guidPkFromDatabase(DataNode node, DbEntity entity) throws SQLException {
142+
protected String guidPkFromDatabase(DataNode node, DbEntity entity) {
143143
try (Connection con = node.getDataSource().getConnection()) {
144144
try (Statement st = con.createStatement()) {
145145
adapter.getJdbcEventLogger().log(SELECT_NEW_GUID);
@@ -150,6 +150,8 @@ protected String guidPkFromDatabase(DataNode node, DbEntity entity) throws SQLEx
150150
return rs.getString(1);
151151
}
152152
}
153+
} catch (SQLException e) {
154+
throw new CayenneRuntimeException("Error generating pk for DbEntity %s", e, entity.getName());
153155
}
154156
}
155157
}

cayenne/src/main/java/org/apache/cayenne/dba/sybase/SybasePkGenerator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.sql.CallableStatement;
3131
import java.sql.Connection;
3232
import java.sql.ResultSet;
33+
import java.sql.SQLException;
3334
import java.util.ArrayList;
3435
import java.util.List;
3536

@@ -98,7 +99,7 @@ protected String pkTableCreateString() {
9899
* node that provides access to a DataSource.
99100
*/
100101
@Override
101-
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
102+
public void createAutoPk(DataNode node, List<DbEntity> dbEntities) {
102103
super.createAutoPk(node, dbEntities);
103104
runUpdate(node, safePkProcDrop());
104105
runUpdate(node, unsafePkProcCreate());
@@ -138,7 +139,7 @@ public List<String> createAutoPkStatements(List<DbEntity> dbEntities) {
138139
* node that provides access to a DataSource.
139140
*/
140141
@Override
141-
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) throws Exception {
142+
public void dropAutoPk(DataNode node, List<DbEntity> dbEntities) {
142143
runUpdate(node, safePkProcDrop());
143144
runUpdate(node, safePkTableDrop());
144145
}
@@ -155,7 +156,7 @@ public List<String> dropAutoPkStatements(List<DbEntity> dbEntities) {
155156
* @since 3.0
156157
*/
157158
@Override
158-
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
159+
protected long longPkFromDatabase(DataNode node, DbEntity entity) {
159160
// handle CAY-588 - get connection that is separate from the connection
160161
// in the current transaction.
161162

@@ -187,6 +188,8 @@ protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Excepti
187188
+ ", no result set from stored procedure.", entity.getName());
188189
}
189190
}
191+
} catch (SQLException e) {
192+
throw new CayenneRuntimeException("Error generating pk for DbEntity %s", e, entity.getName());
190193
} finally {
191194
BaseTransaction.bindThreadTransaction(transaction);
192195
}

0 commit comments

Comments
 (0)