Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 31633 invoked from network); 4 Oct 2006 22:25:27 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 4 Oct 2006 22:25:27 -0000 Received: (qmail 1716 invoked by uid 500); 4 Oct 2006 22:25:27 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 1689 invoked by uid 500); 4 Oct 2006 22:25:27 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 1678 invoked by uid 99); 4 Oct 2006 22:25:27 -0000 Received: from idunn.apache.osuosl.org (HELO idunn.apache.osuosl.org) (140.211.166.84) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 04 Oct 2006 15:25:27 -0700 X-ASF-Spam-Status: No, hits=0.0 required=5.0 tests= Received: from [192.87.106.226] ([192.87.106.226:52534] helo=ajax.apache.org) by idunn.apache.osuosl.org (ecelerity 2.1.1.8 r(12930)) with ESMTP id 41/C5-20288-5D434254 for ; Wed, 04 Oct 2006 15:25:26 -0700 Received: from ajax.apache.org (localhost [127.0.0.1]) by ajax.apache.org (Postfix) with ESMTP id B6326D495A for ; Wed, 4 Oct 2006 23:25:22 +0100 (BST) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Apache Wiki To: derby-commits@db.apache.org Date: Wed, 04 Oct 2006 22:25:22 -0000 Message-ID: <20061004222522.1282.38197@ajax.apache.org> Subject: [Db-derby Wiki] Update of "DatabaseManagerSource" by DonaldMcLean X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Dear Wiki user, You have subscribed to a wiki page or wiki category on "Db-derby Wiki" for change notification. The following page has been changed by DonaldMcLean: http://wiki.apache.org/db-derby/DatabaseManagerSource The comment on the change is: Added new initialization feature ------------------------------------------------------------------------------ */ package your.package.here; + import org.jdom.Document; + import org.jdom.Element; + import org.hibernate.Session; import org.hibernate.SessionFactory; - import org.hibernate.cfg.Configuration; - - import org.jdom.Document; - import org.jdom.Element; - - import java.sql.Driver; - import java.sql.Connection; - import java.sql.SQLException; - import java.sql.Statement; import java.io.File; import java.io.IOException; - + import java.sql.SQLException; + import java.sql.Connection; + import java.sql.Statement; + import java.sql.Driver; + import java.sql.PreparedStatement; + import java.util.List; import java.util.HashSet; - import java.util.List; import java.util.Properties; - /** - * Created: - * Date: Sep 5, 2005 - * Time: 1:57:13 AM - */ public class DatabaseManager { - public DatabaseManager(File dbFile) + public DatabaseManager(String databaseName) { + instance = this; + + Document document = StaugUtilities.getXMLResource(getClass()); + Element root = document.getRootElement(); + + String dbRoot = root.getChildText("Root"); + File dbFile = new File(dbRoot + databaseName); + connectToDatabase(dbFile); - Document document = DatabaseUtilities.getXMLResource(getClass()); - Element root = document.getRootElement(); - if (createFlag) { createTables(root); + runInitializations(root); } createHibernateSessionFactory(root); - - instance = this; } public static DatabaseManager getInstance() { + if (instance == null) + { + new DatabaseManager("LunchtimeDB"); + } + return instance; } @@ -96, +98 @@ return mainSession; } + public void addListener (DBEventListener listener) + { + listeners.add(listener); + } + public void close() { sessions.close(); + sessions = null; try { connection.close(); + connection = null; } catch (SQLException e) { System.out.println("[DatabaseManager.close] Unable to close connection."); } + + instance = null; + + fireEvent(); + listeners = null; } private Connection connection; @@ -114, +128 @@ private Session mainSession = null; private boolean createFlag = false; + private HashSet listeners = new HashSet(); + private void createTables(Element root) { Element createElement = root.getChild("createSql"); @@ -123, +139 @@ try { Statement statement = connection.createStatement(); + connection.setAutoCommit(false); for (int i = 0; i < createElements.size(); i++) { Element element = (Element) createElements.get(i); sql = element.getText(); statement.execute(sql); } + + connection.commit(); + connection.setAutoCommit(true); } catch (SQLException e) { @@ -138, +158 @@ } } + private void runInitializations(Element root) + { + try + { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement("call SYSCS_UTIL.SYSCS_IMPORT_DATA " + + "(null, ?, ?, null, ?, null, null, null, 0)"); + + Element initializations = root.getChild("Initialization"); + + List loadEntries = initializations.getChildren("Load"); + for (int i = 0; i < loadEntries.size(); i++) + { + Element next = (Element) loadEntries.get(i); + + statement.setString(1, next.getChildText("Table")); + statement.setString(2, next.getChildText("Columns")); + String fileName = next.getChildText("File"); + File file = new File(initDirectory, fileName); + if (! file.exists()) + { + System.out.println("[DatabaseManager.runInitializations] file '" + + file.getAbsolutePath() + "' not found."); + continue; + } + statement.setString(3, file.getAbsolutePath()); + + boolean result = statement.execute(); + connection.commit(); + } + + connection.commit(); + connection.setAutoCommit(true); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + private void connectToDatabase(File dbFile) { try @@ -147, +207 @@ } catch (Exception e) { - System.out.println("[ApplicationDatabase : ApplicationDatabase]:" + + System.out.println("[ApplicationDatabase : ApplicationDatabase]: unable to create database."); - " unable to create database."); e.printStackTrace(); } } /** - * @param dbFile - * @throws IOException + * @param dbFile file where the database will be opened. + * @throws java.io.IOException - shouldn't occur. + * @return String URL for the given file. */ private String createURL(File dbFile) throws IOException @@ -184, +244 @@ for(Class aClass : classes) { - System.out.println("[ApplicationDatabase.createHibernateSessionFactory]" + + System.out.println("[ApplicationDatabase.createHibernateSessionFactory] adding clas '" + - " adding class '" + aClass.getName() + "'."); + aClass.getName() + "'."); cfg.addClass(aClass); } @@ -202, +262 @@ for (int i = 0; i < classElements.size(); i++) { Element element = (Element) classElements.get(i); - result.add(DatabaseUtilities.loadClass(element.getText())); + result.add(StaugUtilities.loadClass(element.getText())); } return result; } + private void fireEvent() + { + for(DBEventListener listener : listeners) + { + listener.dbClose(); + } + } + + static private File initDirectory; static private Driver driver; static private DatabaseManager instance = null; + public static void setContext(String contextName) + { + // this function set the 'initDirectory' to the location of the Inititialization files. + throw new IllegalStateException("You must implement this function."); + } + static { try { - driver = (Driver) Class.forName("org.apache.derby.jdbc.EmbeddedDriver") + driver = (Driver) Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); - .newInstance(); System.out.println("[ApplicationDatabase : static]: driver loaded successfuly."); } catch (Exception e) @@ -227, +301 @@ } } + }}}