Finished up liaison stuff.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@324 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user