Mercurial Hosting > nabble
diff src/fschmidt/db/pool/DbDatabaseImpl.java @ 68:00520880ad02
add fschmidt source
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Oct 2025 17:24:15 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fschmidt/db/pool/DbDatabaseImpl.java Sun Oct 05 17:24:15 2025 -0600 @@ -0,0 +1,134 @@ +/* +Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +package fschmidt.db.pool; + +import fschmidt.db.DbDatabase; +import fschmidt.db.SQLRuntimeException; +import fschmidt.db.extend.DbDatabaseExt; +import fschmidt.db.extend.DbTransaction; +import fschmidt.db.extend.FilterDatabase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.Map; +import java.util.Properties; + + +public class DbDatabaseImpl extends FilterDatabase { + private static final Logger logger = LoggerFactory.getLogger(DbDatabaseImpl.class); + + final Pool pool; + final String user; + + public DbDatabaseImpl(DbDatabase database,Pool pool,String user) { + super((DbDatabaseExt)database); + this.pool = pool; + this.user = user; + } + + public DbDatabaseImpl(DbDatabase database) { + this(database,new Pool(),null); + } + + DbDatabase database() { + return database; + } + + @Override public boolean isInTransaction() { + return pool.isInTransaction(); + } + + @Override public void beginTransaction() + throws SQLRuntimeException + { + if( isInTransaction() ) + throw new IllegalStateException("beginTransaction called in nested transaction"); + try { + Connection con = getConnection(); + con.setAutoCommit(false); + } catch(SQLException e) { + throw new SQLRuntimeException(e); + } + } + + @Override public void commitTransaction() { + pool.commitTransaction(); + } + + @Override public void endTransaction() { + pool.endTransaction(); + } + + @Override public void runBeforeCommit(Runnable r) { + if( isInTransaction() ) { + pool.runBeforeCommit(r); + } else { + super.runBeforeCommit(r); + } + } + + @Override public void runJustAfterCommit(Runnable r) { + if( isInTransaction() ) { + pool.runJustAfterCommit(r); + } else { + super.runJustAfterCommit(r); + } + } + + @Override public void runAfterCommit(Runnable r) { + if( isInTransaction() ) { + pool.runAfterCommit(r); + } else { + super.runAfterCommit(r); + } + } + + @Override public void ignoreRunnablesInThisTransaction() { + pool.ignoreRunnablesInThisTransaction(); + } + + @Override public DbTransaction getTransaction() { + return pool.getTransaction(); + } + + @Override public Connection getConnection() + throws SQLException + { + return pool.getConnection(this); + } +}