001/* 
002 * JKNIV, whinstone one contract to access your database.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.whinstone.jdbc;
021
022import java.sql.Connection;
023
024import javax.sql.DataSource;
025
026import net.sf.jkniv.sqlegance.transaction.Isolation;
027import net.sf.jkniv.whinstone.ConnectionAdapter;
028import net.sf.jkniv.whinstone.transaction.TransactionContext;
029import net.sf.jkniv.whinstone.transaction.TransactionSessions;
030
031public class DataSourceAdapter extends AbstractJdbcAdapter
032{
033    private DataSource dataSource;
034    private Isolation  defaultIsolation;
035    
036    public DataSourceAdapter(DataSource ds, String contextName)
037    {
038        super(contextName);
039        this.dataSource = ds;
040        this.defaultIsolation = Isolation.DEFAULT;
041    }
042    
043    /**
044     * Attempts to establish a connection to the database 
045     * @return a Connection from DataSource
046     * @throws net.sf.jkniv.sqlegance.RepositoryException if cannot establish a connection
047     */
048    public ConnectionAdapter open()
049    {
050        return open(defaultIsolation);
051    }
052    
053    /**
054     * Attempts to establish a connection to the database with specific isolation 
055     * @param isolation transaction level for connection
056     * @return a Connection from DataSource
057     * @throws net.sf.jkniv.sqlegance.RepositoryException if cannot establish a connection
058     */
059    public ConnectionAdapter open(Isolation isolation)
060    {
061        ConnectionAdapter adapter = getConnection(isolation);
062        return adapter;
063    }
064    
065    private ConnectionAdapter getConnection(Isolation isolation)
066    {
067        ConnectionAdapter adapter = null;
068        TransactionContext transactionContext = TransactionSessions.get(this.contextName);
069        
070        if (transactionContext != null && transactionContext.isActive())
071        {
072            LOG.debug("Taking existent Connection from Transaction Context");
073            adapter = transactionContext.getConnection();
074        }
075        if (adapter == null)
076        {
077            try
078            {
079                LOG.trace("Getting new connection from DataSource");
080                Connection jdbcConn = dataSource.getConnection();
081                setIsolation(jdbcConn, isolation);
082                adapter = new JdbcConnectionAdapter(contextName, jdbcConn, handlerException);
083            }
084            catch (Exception e)//SQLException
085            {
086                handlerException.handle(e, "SEVERE FAIL, cannot get database connection datasource [" + dataSource
087                        + "] Reason: " + e.getMessage());
088            }
089        }
090        return adapter;
091    }
092    
093}