You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by Apache Wiki <wi...@apache.org> on 2006/10/05 00:25:22 UTC
[Db-derby Wiki] Update of "DatabaseManagerSource" by DonaldMcLean
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<DBEventListener> listeners = new HashSet<DBEventListener>();
+
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 @@
}
}
+
}}}