package jp.sf.amateras.mirage.integration.spring;

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

import jp.sf.amateras.mirage.provider.ConnectionProvider;

import org.springframework.jdbc.datasource.ConnectionHolder;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * {@link ConnectionProvider} implementation to integrate Mirage to Spring Framework.
 *
 * @author Naoki Takezoe
 */
public class SpringConnectionProvider implements ConnectionProvider {

	private DataSourceTransactionManager transactionManager;

	public void setTransactionManager(
			DataSourceTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

	@Override
	public Connection getConnection() throws SQLException {
		ConnectionHolder conHolder =
		    (ConnectionHolder) TransactionSynchronizationManager.getResource(
		    		transactionManager.getDataSource());

		return conHolder.getConnection();
	}

}
