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; 023import java.sql.DriverManager; 024import java.util.Properties; 025 026import net.sf.jkniv.sqlegance.RepositoryProperty; 027import net.sf.jkniv.sqlegance.transaction.Isolation; 028import net.sf.jkniv.whinstone.ConnectionAdapter; 029import net.sf.jkniv.whinstone.transaction.TransactionContext; 030import net.sf.jkniv.whinstone.transaction.TransactionSessions; 031 032public class DriverManagerAdapter extends AbstractJdbcAdapter 033{ 034 private Properties props; 035 private Isolation defaultIsolation; 036 private String driver; 037 private String url; 038 039 public DriverManagerAdapter(Properties props, String contextName)//(Properties props, Isolation defaultIsolation, String name) 040 { 041 super(contextName); 042 this.props = props; 043 this.defaultIsolation = Isolation.DEFAULT; 044 this.driver = props.getProperty(RepositoryProperty.JDBC_DRIVER.key()); 045 this.url = props.getProperty(RepositoryProperty.JDBC_URL.key()); 046 if (driver != null) 047 register(); 048 } 049 050 /** 051 * Attempts to establish a connection to the database 052 * @return a Connection to the URL 053 * @throws net.sf.jkniv.sqlegance.RepositoryException if cannot establish a connection 054 */ 055 public ConnectionAdapter open() 056 { 057 return open(defaultIsolation); 058 } 059 060 /** 061 * Attempts to establish a connection to the database with specific isolation 062 * @param isolation transaction level for connection 063 * @return a Connection to the URL 064 * @throws net.sf.jkniv.sqlegance.RepositoryException if cannot establish a connection 065 */ 066 public ConnectionAdapter open(Isolation isolation) 067 { 068 ConnectionAdapter adapter = getConnection(isolation); 069 return adapter; 070 } 071 072 private ConnectionAdapter getConnection(Isolation isolation) 073 { 074 ConnectionAdapter adapter = null; 075 TransactionContext transactionContext = TransactionSessions.get(this.contextName); 076 077 if (transactionContext != null && transactionContext.isActive()) 078 { 079 LOG.debug("Taking existent Connection from Transaction Context"); 080 adapter = transactionContext.getConnection(); 081 } 082 if (adapter == null) 083 { 084 try 085 { 086 LOG.debug("Getting new connection from DriverManager"); 087 Connection jdbcConn = DriverManager.getConnection(url, props); 088 setIsolation(jdbcConn, isolation); 089 adapter = new JdbcConnectionAdapter(contextName, jdbcConn, this.handlerException); 090 } 091 catch (Exception e)//SQLException 092 { 093 handlerException.handle(e, 094 "SEVERE FAIL, cannot get database connection url [" + url + "] config=" + props); 095 } 096 097 } 098 return adapter; 099 } 100 101 private void register() 102 { 103 try 104 { 105 Class.forName(driver); 106 } 107 catch (ClassNotFoundException e) 108 { 109 new net.sf.jkniv.reflect.ReflectionException("ClassNotFoundException for register ["+driver+"]", e); 110 } 111 } 112}