|
Spring Framework example source code file (ConnectionHolder.java)
The Spring Framework ConnectionHolder.java source code
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.datasource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import org.springframework.transaction.support.ResourceHolderSupport;
import org.springframework.util.Assert;
/**
* Connection holder, wrapping a JDBC Connection.
* {@link DataSourceTransactionManager} binds instances of this class
* to the thread, for a specific DataSource.
*
* <p>Inherits rollback-only support for nested JDBC transactions
* and reference count functionality from the base class.
*
* <p>Note: This is an SPI class, not intended to be used by applications.
*
* @author Juergen Hoeller
* @since 06.05.2003
* @see DataSourceTransactionManager
* @see DataSourceUtils
*/
public class ConnectionHolder extends ResourceHolderSupport {
public static final String SAVEPOINT_NAME_PREFIX = "SAVEPOINT_";
private ConnectionHandle connectionHandle;
private Connection currentConnection;
private boolean transactionActive = false;
private Boolean savepointsSupported;
private int savepointCounter = 0;
/**
* Create a new ConnectionHolder for the given ConnectionHandle.
* @param connectionHandle the ConnectionHandle to hold
*/
public ConnectionHolder(ConnectionHandle connectionHandle) {
Assert.notNull(connectionHandle, "ConnectionHandle must not be null");
this.connectionHandle = connectionHandle;
}
/**
* Create a new ConnectionHolder for the given JDBC Connection,
* wrapping it with a {@link SimpleConnectionHandle},
* assuming that there is no ongoing transaction.
* @param connection the JDBC Connection to hold
* @see SimpleConnectionHandle
* @see #ConnectionHolder(java.sql.Connection, boolean)
*/
public ConnectionHolder(Connection connection) {
this.connectionHandle = new SimpleConnectionHandle(connection);
}
/**
* Create a new ConnectionHolder for the given JDBC Connection,
* wrapping it with a {@link SimpleConnectionHandle}.
* @param connection the JDBC Connection to hold
* @param transactionActive whether the given Connection is involved
* in an ongoing transaction
* @see SimpleConnectionHandle
*/
public ConnectionHolder(Connection connection, boolean transactionActive) {
this(connection);
this.transactionActive = transactionActive;
}
/**
* Return the ConnectionHandle held by this ConnectionHolder.
*/
public ConnectionHandle getConnectionHandle() {
return this.connectionHandle;
}
/**
* Return whether this holder currently has a Connection.
*/
protected boolean hasConnection() {
return (this.connectionHandle != null);
}
/**
* Set whether this holder represents an active, JDBC-managed transaction.
* @see DataSourceTransactionManager
*/
protected void setTransactionActive(boolean transactionActive) {
this.transactionActive = transactionActive;
}
/**
* Return whether this holder represents an active, JDBC-managed transaction.
*/
protected boolean isTransactionActive() {
return this.transactionActive;
}
/**
* Override the existing Connection handle with the given Connection.
* Reset the handle if given <code>null.
* <p>Used for releasing the Connection on suspend (with a
Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework ConnectionHolder.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.