From 33e39679dacefff424fa2b3235c01cab802805b1 Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 20 Sep 2001 02:09:09 +0000 Subject: [PATCH] Finished up liaison stuff. git-svn-id: https://samskivert.googlecode.com/svn/trunk@324 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/jdbc/DatabaseLiaison.java | 11 ++-- .../com/samskivert/jdbc/DefaultLiaison.java | 56 +++++++++++++++++++ .../com/samskivert/jdbc/LiaisonRegistry.java | 51 ++++++++++++++++- .../com/samskivert/jdbc/MySQLLiaison.java | 6 +- .../com/samskivert/jdbc/SimpleRepository.java | 43 +++++++------- 5 files changed, 137 insertions(+), 30 deletions(-) create mode 100644 projects/samskivert/src/java/com/samskivert/jdbc/DefaultLiaison.java diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/DatabaseLiaison.java b/projects/samskivert/src/java/com/samskivert/jdbc/DatabaseLiaison.java index fa0dc87b..dddeaab6 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/DatabaseLiaison.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/DatabaseLiaison.java @@ -1,5 +1,5 @@ // -// $Id: DatabaseLiaison.java,v 1.1 2001/09/20 01:53:19 mdb Exp $ +// $Id: DatabaseLiaison.java,v 1.2 2001/09/20 02:09:09 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -33,12 +33,13 @@ import java.sql.SQLException; public interface DatabaseLiaison { /** - * Indicates whether this particular RDBMS/JDBC driver combination - * supports transactions. + * Indicates whether or not this database liaison is the proper + * liaison for the specified database URL. * - * @return true if transactions are supported, false if not. + * @return true if we should use this liaison for connections created + * with the supplied URL, false if we should not. */ - public boolean supportsTransactions (); + public boolean matchesURL (String url); /** * Determines whether or not the supplied SQL exception was caused by diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/DefaultLiaison.java b/projects/samskivert/src/java/com/samskivert/jdbc/DefaultLiaison.java new file mode 100644 index 00000000..afe3bcca --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/jdbc/DefaultLiaison.java @@ -0,0 +1,56 @@ +// +// $Id: DefaultLiaison.java,v 1.1 2001/09/20 02:09:09 mdb Exp $ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.jdbc; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * The default liaison is used if no other liaison could be matched for a + * particular database connection. It isn't very smart or useful but we + * need something. + */ +public class DefaultLiaison implements DatabaseLiaison +{ + // documentation inherited + public boolean matchesURL (String url) + { + return true; + } + + // documentation inherited + public boolean isDuplicateRowException (SQLException sqe) + { + return false; + } + + // documentation inherited + public boolean isTransientException (SQLException sqe) + { + return false; + } + + // documentation inherited + public int lastInsertedId (Connection conn) throws SQLException + { + return -1; + } +} diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/LiaisonRegistry.java b/projects/samskivert/src/java/com/samskivert/jdbc/LiaisonRegistry.java index d8615738..bf11b5a5 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/LiaisonRegistry.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/LiaisonRegistry.java @@ -1,5 +1,5 @@ // -// $Id: LiaisonRegistry.java,v 1.1 2001/09/20 01:53:19 mdb Exp $ +// $Id: LiaisonRegistry.java,v 1.2 2001/09/20 02:09:09 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -22,6 +22,12 @@ package com.samskivert.jdbc; import java.sql.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.HashMap; + +import com.samskivert.Log; + /** * The liaison registry provides access to the appropriate database * liaison implementation for a particular database connection. @@ -33,7 +39,48 @@ public class LiaisonRegistry * connection. */ public static DatabaseLiaison getLiaison (Connection conn) + throws SQLException { - return null; + DatabaseMetaData dmd = conn.getMetaData(); + String url = dmd.getURL(); + + // see if we already have a liaison mapped for this connection + DatabaseLiaison liaison = (DatabaseLiaison)_mappings.get(url); + + if (liaison == null) { + // scan the list looking for a matching liaison + Iterator iter = _liaisons.iterator(); + while (iter.hasNext()) { + DatabaseLiaison candidate = (DatabaseLiaison)iter.next(); + if (candidate.matchesURL(url)) { + liaison = candidate; + break; + } + } + + // if we didn't find a matching liaison, use the default + if (liaison == null) { + liaison = new DefaultLiaison(); + } + + // map this URL to this liaison + _mappings.put(url, liaison); + } + + return liaison; } + + protected static void registerLiaisonClass (Class lclass) + { + // create a new instance and stick it on our list + try { + _liaisons.add(lclass.newInstance()); + } catch (Exception e) { + Log.warning("Unable to instantiate liaison " + + "[class=" + lclass.getName() + ", error=" + e + "]."); + } + } + + protected static ArrayList _liaisons = new ArrayList(); + protected static HashMap _mappings = new HashMap(); } diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/MySQLLiaison.java b/projects/samskivert/src/java/com/samskivert/jdbc/MySQLLiaison.java index 18cda34c..a0d9110f 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/MySQLLiaison.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/MySQLLiaison.java @@ -1,5 +1,5 @@ // -// $Id: MySQLLiaison.java,v 1.1 2001/09/20 01:53:19 mdb Exp $ +// $Id: MySQLLiaison.java,v 1.2 2001/09/20 02:09:09 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -28,9 +28,9 @@ import java.sql.*; public class MySQLLiaison { // documentation inherited - public boolean supportsTransactions () + public boolean matchesURL (String url) { - return false; + return url.startsWith("jdbc:mysql"); } // documentation inherited diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java b/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java index 0dc2de64..cd51533b 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/SimpleRepository.java @@ -1,5 +1,5 @@ // -// $Id: SimpleRepository.java,v 1.1 2001/09/20 01:53:20 mdb Exp $ +// $Id: SimpleRepository.java,v 1.2 2001/09/20 02:09:09 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -77,19 +77,22 @@ public class SimpleRepository extends Repository protected Object execute (Operation op, boolean retryOnTransientFailure) throws PersistenceException { - // obtain our database connection Connection conn = null; + DatabaseMetaData dmd = null; + DatabaseLiaison liaison = null; + + // obtain our database connection and associated goodies try { conn = _provider.getConnection(_dbident); conn.setAutoCommit(false); + dmd = conn.getMetaData(); + liaison = LiaisonRegistry.getLiaison(conn); gotConnection(conn); } catch (SQLException sqe) { String err = "Unable to obtain connection."; throw new PersistenceException(err, sqe); } - // obtain a liaison for this connection - DatabaseLiaison liaison = LiaisonRegistry.getLiaison(conn); Object rv = null; try { @@ -97,7 +100,7 @@ public class SimpleRepository extends Repository rv = op.invoke(conn, liaison); // commit the transaction - if (liaison.supportsTransactions()) { + if (dmd.supportsTransactions()) { conn.commit(); } @@ -106,13 +109,13 @@ public class SimpleRepository extends Repository } catch (SQLException sqe) { // back out our changes if something got hosed - if (liaison.supportsTransactions()) { - try { + try { + if (dmd.supportsTransactions()) { conn.rollback(); - } catch (SQLException rbe) { - Log.warning("Unable to roll back operation."); - Log.logStackTrace(rbe); } + } catch (SQLException rbe) { + Log.warning("Unable to roll back operation."); + Log.logStackTrace(rbe); } // let the connection provider know that the connection failed @@ -134,25 +137,25 @@ public class SimpleRepository extends Repository } catch (PersistenceException pe) { // back out our changes if something got hosed - if (liaison.supportsTransactions()) { - try { + try { + if (dmd.supportsTransactions()) { conn.rollback(); - } catch (SQLException rbe) { - Log.warning("Unable to roll back operation."); - Log.logStackTrace(rbe); } + } catch (SQLException rbe) { + Log.warning("Unable to roll back operation."); + Log.logStackTrace(rbe); } throw pe; } catch (RuntimeException rte) { // back out our changes if something got hosed - if (conn != null && liaison.supportsTransactions()) { - try { + try { + if (conn != null && dmd.supportsTransactions()) { conn.rollback(); - } catch (SQLException rbe) { - Log.warning("Unable to roll back operation."); - Log.logStackTrace(rbe); } + } catch (SQLException rbe) { + Log.warning("Unable to roll back operation."); + Log.logStackTrace(rbe); } throw rte;