From 4da25535819b4256c184827ef42d592bb9844391 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 13 Feb 2001 05:55:57 +0000 Subject: [PATCH] Catch and report class not found when loading the JDBC driver. Added an execute() helper function which will automatically commit or roll back an SQL operation if it succeeds or fails. git-svn-id: https://samskivert.googlecode.com/svn/trunk@42 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/jdbc/Repository.java | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/Repository.java b/projects/samskivert/src/java/com/samskivert/jdbc/Repository.java index 6afb79af..f02474be 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/Repository.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/Repository.java @@ -1,5 +1,5 @@ // -// $Id: Repository.java,v 1.1 2001/02/13 00:25:14 mdb Exp $ +// $Id: Repository.java,v 1.2 2001/02/13 05:55:57 mdb Exp $ package com.samskivert.jdbc; @@ -8,7 +8,7 @@ import java.util.List; import java.util.Properties; import com.samskivert.util.*; -import jora.*; +import com.samskivert.jdbc.jora.*; /** * The repository class provides basic functionality upon which to build @@ -49,7 +49,11 @@ public abstract class Repository requireProp(props, "username", "No driver username specified."); String password = requireProp(props, "password", "No driver password specified."); - _session.open(url, username, password); + + // the only reason session.open() fails is class not found + if (!_session.open(url, username, password)) { + throw new SQLException("Unable to load JDBC driver class."); + } // set auto-commit to false _session.connection.setAutoCommit(false); @@ -89,5 +93,42 @@ public abstract class Repository _session.close(); } + /** + * Used by execute. + * + * @see #execute + */ + protected interface Operation + { + public void invoke () throws SQLException; + } + + /** + * Executes the supplied operation followed by a call to commit() on + * the session unless an SQLException or runtime error occurs, in + * which case a call to rollback() is executed on the session. + */ + protected void execute (Operation op) + throws SQLException + { + try { + // invoke the operation + op.invoke(); + + // commit the session + _session.commit(); + + } catch (SQLException sqe) { + // back out our changes if something got hosed + _session.rollback(); + throw sqe; + + } catch (RuntimeException rte) { + // back out our changes if something got hosed + _session.rollback(); + throw rte; + } + } + protected Session _session; }