Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.j256.ormlite.jdbc;
  
  
  
Wrapper around a javax.sql.DataSource that supports our ConnectionSource interface. This allows you to wrap other multi-threaded, high-performance data sources, see Apache DBCP, CP30, or BoneCP.

NOTE: If you are using the Spring type wiring in Java, initialize() should be called after all of the set methods. In Spring XML, init-method="initialize" should be used.

Author(s):
graywatson
 
 public class DataSourceConnectionSource extends BaseConnectionSource implements ConnectionSource {
 
 	private static Logger logger = LoggerFactory.getLogger(DataSourceConnectionSource.class);
 
 	private DataSource dataSource;
 	private String databaseUrl;
 	private boolean initialized = false;

Constructor for Spring type wiring if you are using the set methods. If you are using Spring then your should use: init-method="initialize"
 
 		// for spring type wiring
 	}

Create a data source wrapper for a DataSource.

Throws:
java.sql.SQLException If the driver associated with the database URL is not found in the classpath.
 
 	public DataSourceConnectionSource(DataSource dataSourceString databaseUrlthrows SQLException {
 		this. = dataSource;
 		this. = databaseUrl;
 	}

Create a data source wrapper for a DataSource. The databaseType is usually determined from the databaseUrl so most users should call DataSourceConnectionSource(javax.sql.DataSource,java.lang.String) instead. If, however, you need to force the class to use a specific DatabaseType then this constructor should be used.

Throws:
java.sql.SQLException If the driver associated with the database URL is not found in the classpath.
 
 	public DataSourceConnectionSource(DataSource dataSourceDatabaseType databaseTypethrows SQLException {
 		this. = dataSource;
 		this. = databaseType;
 	}

Initialize the class after the setters have been called. If you are using the no-arg constructor and Spring type wiring, this should be called after all of the set methods.

Throws:
java.sql.SQLException If the driver associated with the database URL is not found in the classpath.
 
 	public void initialize() throws SQLException {
 		if () {
 			return;
 		}
 		if ( == null) {
 			throw new IllegalStateException("dataSource was never set on " + getClass().getSimpleName());
 		}
 		if ( == null) {
 			if ( == null) {
 				throw new IllegalStateException("either the databaseUri or the databaseType must be set on "
 			}
 			 = DatabaseTypeUtils.createDatabaseType();
 		}
 		if ( != null) {
 		}
 		 = true;
 	}
 
 		if (!) {
			throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
		}
	}
	public DatabaseConnection getReadOnlyConnection(String usernameString passwordthrows SQLException {
		if (!) {
			throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
		}
		return getReadWriteConnection(usernamepassword);
	}
		if (!) {
			throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
		}
		if (saved != null) {
			return saved;
		}
	}
	public void releaseConnection(DatabaseConnection connectionthrows SQLException {
		if (!) {
			throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
		}
		if (isSavedConnection(connection)) {
			// ignore the release because we will close it at the end of the connection
else {
			connection.close();
		}
	}
	public DatabaseConnection getReadWriteConnection(String usernameString passwordthrows SQLException {
		if (!) {
			throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
		}
		if (saved != null) {
			return saved;
		}
		return new JdbcDatabaseConnection(.getConnection(usernamepassword));
	}
	public boolean saveSpecialConnection(DatabaseConnection connectionthrows SQLException {
		/*
		 * This is fine to not be synchronized since it is only this thread we care about. Other threads will set this
		 * or have it synchronized in over time.
		 */
		return saveSpecial(connection);
	}
	public void clearSpecialConnection(DatabaseConnection connection) {
		clearSpecial(connection);
	}

This typically closes the connection source but because there is not a close() method on the javax.sql.DataSource (grrrr), this close method does _nothing_. You must close the underlying data-source yourself.
	public void close() throws SQLException {
		if (!) {
			throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
		}
		// unfortunately, you will need to close the DataSource directly since there is no close on the interface
	}
		if (!) {
			throw new IllegalStateException(getClass().getSimpleName() + ".initialize() was not called");
		}
		return ;
	}

Unfortunately we cannot tell if the related data source has been closed so this just returns true.
	public boolean isOpen() {
		return true;
	}
	public void setDataSource(DataSource dataSource) {
		this. = dataSource;
	}
	public void setDatabaseType(DatabaseType databaseType) {
		this. = databaseType;
	}
	public void setDatabaseUrl(String databaseUrl) {
		this. = databaseUrl;
	}

Deprecated:
No longer supported and can be removed.
	public void setUsesTransactions(boolean usesTransactions) {
	}
New to GrepCode? Check out our FAQ X