String tableName = storeMgr.getStringProperty("datanucleus.rdbms.schemaTable.tableName");
schemaTable = new SchemaTable(storeMgr, tableName);
schemaTable.initialize(clr);
// We need to autocreate the table and validate it being correct.
ManagedConnection mconn = storeMgr.getConnection(UserTransaction.TRANSACTION_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.close();
}
}