001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024 package org.jboss.dna.connector.jdbc;
025
026 import java.sql.Connection;
027 import java.sql.SQLException;
028 import java.util.UUID;
029 import java.util.concurrent.TimeUnit;
030 import javax.sql.XAConnection;
031 import javax.transaction.xa.XAResource;
032 import org.jboss.dna.common.util.Logger;
033 import org.jboss.dna.graph.ExecutionContext;
034 import org.jboss.dna.graph.cache.CachePolicy;
035 import org.jboss.dna.graph.connector.RepositoryConnection;
036 import org.jboss.dna.graph.connector.RepositorySourceException;
037 import org.jboss.dna.graph.request.Request;
038 import org.jboss.dna.graph.request.processor.RequestProcessor;
039
040 /**
041 * JDBC connection wrapper
042 *
043 * @author <a href="mailto:litsenko_sergey@yahoo.com">Sergiy Litsenko</a>
044 */
045 public class JdbcConnection implements RepositoryConnection {
046 /**
047 * Logging for this instance
048 */
049 protected Logger log = Logger.getLogger(getClass());
050
051 private final String name;
052 private final CachePolicy cachePolicy;
053 private final Connection connection;
054 private final UUID rootNodeUuid;
055
056 /*package*/JdbcConnection( String sourceName,
057 CachePolicy cachePolicy,
058 Connection connection,
059 UUID rootNodeUuid ) {
060 assert sourceName != null;
061 assert connection != null;
062 assert rootNodeUuid != null;
063 this.name = sourceName;
064 this.cachePolicy = cachePolicy; // may be null
065 this.connection = connection;
066 this.rootNodeUuid = rootNodeUuid;
067 }
068
069 /**
070 * {@inheritDoc}
071 *
072 * @see org.jboss.dna.graph.connector.RepositoryConnection#getSourceName()
073 */
074 public String getSourceName() {
075 return name;
076 }
077
078 /**
079 * {@inheritDoc}
080 *
081 * @see org.jboss.dna.graph.connector.RepositoryConnection#getDefaultCachePolicy()
082 */
083 public CachePolicy getDefaultCachePolicy() {
084 return cachePolicy;
085 }
086
087 /**
088 * {@inheritDoc}
089 *
090 * @see org.jboss.dna.graph.connector.RepositoryConnection#getXAResource()
091 */
092 public XAResource getXAResource() {
093 // if implemented by JDBC driver
094 if (connection instanceof XAConnection) {
095 try {
096 return ((XAConnection)connection).getXAResource();
097 } catch (SQLException e) {
098 // handle an exception silently so far and write it to the log
099 log.error(e, JdbcMetadataI18n.unableToGetXAResource, getSourceName());
100 return null;
101 }
102 }
103 // default
104 return null;
105 }
106
107 /**
108 * {@inheritDoc}
109 *
110 * @see org.jboss.dna.graph.connector.RepositoryConnection#ping(long, java.util.concurrent.TimeUnit)
111 */
112 public boolean ping( long time,
113 TimeUnit unit ) {
114 try {
115 // JDBC 4 has a method to check validity of a connection (connection.isValid(timeout))
116 // but many drivers didn't get updated with latest spec
117 return connection != null && !connection.isClosed();
118 } catch (SQLException e) {
119 // debug
120 if (log.isDebugEnabled()) {
121 log.debug(e, "{0}: Unable to check database connection due to error.", getSourceName());
122 }
123 return false;
124 }
125 }
126
127 /**
128 * {@inheritDoc}
129 *
130 * @see org.jboss.dna.graph.connector.RepositoryConnection#execute(org.jboss.dna.graph.ExecutionContext,
131 * org.jboss.dna.graph.request.Request)
132 */
133 public void execute( ExecutionContext context,
134 Request request ) throws RepositorySourceException {
135 // create processor and delegate handling
136 RequestProcessor proc = new JdbcRequestProcesor(getSourceName(), context, connection, rootNodeUuid);
137 try {
138 proc.process(request);
139 } finally {
140 proc.close();
141 }
142 }
143
144 /**
145 * {@inheritDoc}
146 *
147 * @see org.jboss.dna.graph.connector.RepositoryConnection#close()
148 */
149 public void close() {
150 try {
151 // release the JDBC connection resource
152 if (connection != null && !connection.isClosed()) {
153 connection.close();
154 }
155 } catch (Exception e) {
156 // handle exception silently so far
157 if (log.isDebugEnabled()) {
158 log.debug(e, "{0}: Unable to close database connection due to error.", getSourceName());
159 }
160 }
161 }
162
163 }