EOAdaptorContext adaptorContext = databaseContext.adaptorContext();
NSMutableArray beforeOpenChannels = new NSMutableArray();
Enumeration beforeChannelsEnum = adaptorContext.channels().objectEnumerator();
while (beforeChannelsEnum.hasMoreElements()) {
EOAdaptorChannel channel = (EOAdaptorChannel) beforeChannelsEnum.nextElement();
if (channel.isOpen()) {
beforeOpenChannels.addObject(channel);
}
}
try {
EODatabaseChannel databaseChannel = databaseContext.availableChannel();
EOAdaptorChannel adaptorChannel = databaseChannel.adaptorChannel();
boolean channelOpen = adaptorChannel.isOpen();
if (!channelOpen) {
adaptorChannel.openChannel();
}
try {
JDBCContext jdbccontext = (JDBCContext) adaptorChannel.adaptorContext();
try {
jdbccontext.beginTransaction();
Connection conn = jdbccontext.connection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.commit();
} catch (SQLException sqlexception) {
sqlexception.printStackTrace(System.out);
jdbccontext.rollbackTransaction();
throw sqlexception;
}
} finally {
if (!channelOpen) {
adaptorChannel.closeChannel();
}
}
} finally {
Enumeration afterChannelsEnum = adaptorContext.channels().objectEnumerator();
while (afterChannelsEnum.hasMoreElements()) {
EOAdaptorChannel channel = (EOAdaptorChannel) afterChannelsEnum.nextElement();
if (channel.isOpen() && !beforeOpenChannels.containsObject(channel)) {
channel.closeChannel();
}
}
}
}