/
SchemaAutoStarter.java
286 lines (260 loc) · 9.47 KB
/
SchemaAutoStarter.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**********************************************************************
Copyright (c) 2003 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
2004 Andy Jefferson - added type, version
2004 Andy Jefferson - changed to use StoreData. Added owner
...
**********************************************************************/
package org.datanucleus.store.rdbms.autostart;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.store.StoreData;
import org.datanucleus.store.StoreManager;
import org.datanucleus.store.autostart.AbstractAutoStartMechanism;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.exceptions.DatastoreInitialisationException;
import org.datanucleus.store.rdbms.RDBMSPropertyNames;
import org.datanucleus.store.rdbms.RDBMSStoreData;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.store.rdbms.exceptions.MissingTableException;
import org.datanucleus.transaction.TransactionIsolation;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
/**
* Implementation of an Auto-Start Mechanism for DataNucleus.
* This implementation stores the classes supported in a table in the datastore.
* It is initialised and read at startup, and is continually updated during the lifetime of the
* calling application.
*/
public class SchemaAutoStarter extends AbstractAutoStartMechanism
{
private static final Localiser LOCALISER_RDBMS = Localiser.getInstance(
"org.datanucleus.store.rdbms.Localisation", RDBMSStoreManager.class.getClassLoader());
protected SchemaTable schemaTable = null;
protected RDBMSStoreManager storeMgr = null;
protected ManagedConnection mconn;
/**
* Constructor.
* @param store_mgr The RDBMSManager managing the store that we are auto-starting.
* @param clr The ClassLoaderResolver
*/
public SchemaAutoStarter(StoreManager store_mgr, ClassLoaderResolver clr)
{
super();
storeMgr = (RDBMSStoreManager)store_mgr;
// Create the auto-start table in the datastore, using any user-specified name
String tableName = storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_RDBMS_SCHEMA_TABLE_NAME);
schemaTable = new SchemaTable(storeMgr, tableName);
schemaTable.initialize(clr);
// We need to autocreate the table and validate it being correct.
ManagedConnection mconn = storeMgr.getConnection(TransactionIsolation.NONE);
Connection conn = (Connection) mconn.getConnection();
try
{
// Create the table if it doesn't exist, and validate it
schemaTable.exists(conn, true);
if (storeMgr.getDdlWriter() != null)
{
// when DDL is only written to a file instead of being sent to DB, validating it
// will throw a MissingTableException
try
{
schemaTable.validate(conn, true, false, null);
}
catch (MissingTableException mte)
{
// if table had not existed, the DDL for creating it has been written above in
// schemaTable.checkExists(conn,true),
// any other validation exception will be handled below
}
}
else
{
schemaTable.validate(conn, true, false, null);
}
}
catch (Exception e)
{
// Validation error - table is different to what we expect
NucleusLogger.DATASTORE_SCHEMA.error(LOCALISER_RDBMS.msg("049001",storeMgr.getSchemaName(),e));
try
{
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled())
{
NucleusLogger.DATASTORE_SCHEMA.debug(LOCALISER_RDBMS.msg("049002",this.schemaTable.toString()));
}
try
{
// Drop the table in case it had incorrect structure before
schemaTable.drop(conn);
}
catch (SQLException sqe)
{
// Do nothing. Maybe the table didn't exist in the first place
}
// Create the table
schemaTable.exists(conn, true);
schemaTable.validate(conn, true, false, null);
}
catch (Exception e2)
{
NucleusLogger.DATASTORE_SCHEMA.error(LOCALISER_RDBMS.msg("049001",storeMgr.getSchemaName(),e2));
}
}
finally
{
mconn.release();
}
}
/**
* Accessor for the data for the classes supported.
* @return Collection of classes supported (StoreData). Collection of StoreData elements
* @throws DatastoreInitialisationException if an error occurs in datastore communication
*/
public Collection getAllClassData()
throws DatastoreInitialisationException
{
try
{
assertIsOpen();
Collection data=null;
try
{
data = schemaTable.getAllClasses(mconn);
}
catch (SQLException sqe2)
{
NucleusLogger.DATASTORE_SCHEMA.error(LOCALISER_RDBMS.msg("049000",sqe2));
//TODO in case of exception should we throw a Fatal Exception???
}
return data;
}
catch (Exception e)
{
throw new DatastoreInitialisationException(LOCALISER_RDBMS.msg("049010",e),e);
}
}
/**
* Assert that the mechanism is open from writings
*/
private void assertIsOpen()
{
if (mconn == null)
{
throw new NucleusException(LOCALISER_RDBMS.msg("049008")).setFatal();
}
}
/**
* Assert that the mechanism is closed from writing.
*/
private void assertIsClosed()
{
if (mconn != null)
{
throw new NucleusException(LOCALISER_RDBMS.msg("049009")).setFatal();
}
}
/**
* Starts a transaction for writing (add/delete) classes to the auto start mechanism
*/
public void open()
{
assertIsClosed();
mconn = storeMgr.getConnection(TransactionIsolation.NONE);
}
/**
* Closes a transaction for writing (add/delete) classes to the auto start mechanism
*/
public void close()
{
assertIsOpen();
try
{
mconn.release();
mconn = null;
}
catch (NucleusException sqe2)
{
NucleusLogger.DATASTORE_SCHEMA.error(LOCALISER_RDBMS.msg("050005",sqe2));
}
}
/**
* Whether it's open for writing (add/delete) classes to the auto start mechanism
* @return whether this is open for writing
*/
public boolean isOpen()
{
return mconn != null;
}
/**
* Method to add a class to the supported list.
* @param data Data for the class to add.
**/
public void addClass(StoreData data)
{
RDBMSStoreData tableData = (RDBMSStoreData)data; // We only support MappedStoreData
assertIsOpen();
try
{
schemaTable.addClass(tableData, mconn);
}
catch (SQLException sqe2)
{
String msg = LOCALISER_RDBMS.msg("049003", data.getName(), sqe2);
NucleusLogger.DATASTORE_SCHEMA.error(msg);
throw new NucleusDataStoreException(msg, sqe2);
}
}
/**
* Method to drop support for a class.
* @param class_name The class
*/
public void deleteClass(String class_name)
{
assertIsOpen();
try
{
schemaTable.deleteClass(class_name,mconn);
}
catch (SQLException sqe2)
{
NucleusLogger.DATASTORE_SCHEMA.error(LOCALISER_RDBMS.msg("049005", class_name, sqe2));
}
}
/**
* Method to drop support for all current classes.
*/
public void deleteAllClasses()
{
assertIsOpen();
try
{
schemaTable.deleteAllClasses(mconn);
}
catch (SQLException sqe2)
{
NucleusLogger.DATASTORE_SCHEMA.error(LOCALISER_RDBMS.msg("049006", sqe2));
}
}
/**
* Utility to output the storage description for this mechanism.
* @return The storage description
*/
public String getStorageDescription()
{
return LOCALISER_RDBMS.msg("049007", this.schemaTable.toString());
}
}