All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.peterphi.std.guice.hibernatetest.dbunit.HibernateDatabaseConnection Maven / Gradle / Ivy

There is a newer version: 10.1.5
Show newest version
package com.peterphi.std.guice.hibernatetest.dbunit;

import org.dbunit.database.AbstractDatabaseConnection;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;

public class HibernateDatabaseConnection extends AbstractDatabaseConnection
{
	private final ConnectionProvider connectionProvider;
	private final String schema;


	private Connection connection = null;


	public HibernateDatabaseConnection(final ServiceRegistry registry, String schema)
	{
		this.connectionProvider = registry.getService(ConnectionProvider.class);
		this.schema = schema;
	}


	@Override
	public synchronized Connection getConnection() throws SQLException
	{
		if (this.connection == null)
		{
			this.connection = this.connectionProvider.getConnection();
		}

		return this.connection;
	}


	@Override
	public String getSchema()
	{
		return schema;
	}


	@Override
	public synchronized void close() throws SQLException
	{
		if (this.connection != null)
		{
			connectionProvider.closeConnection(this.connection);
			this.connection = null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy