Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC (http://ursaj.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.ursaj.hfs.service;
 
 
Adapter to handle operations over DB.
 
 abstract class HfsDbAdapter {
    
JNDI data source bean name.
 
     public static final String DATA_SOURCE_BEAN_NAME = "dataSource";

    
Class logger.
 
     private static final Logger log = LoggerFactory.getLogger(HfsDbAdapter.class);

    
Persistence data source.
 
     private DataSource dataSource;

    
Gets persistence data source.

Returns:
Persistence data source.
 
     protected DataSource getDataSource() {
         if ( == null) {
             try {
                  = (DataSourcenew InitialContext().lookup(getDataSourceName());
             }
             catch (NamingException e) {
                 throw new IllegalStateException("Failed to lookup data source in local context."e);
             }
         }
 
         return ;
     }

    
Sets persistence data source.

Parameters:
dataSource Persistence data source.
 
     protected void setDataSource(DataSource dataSource) {
         this. = dataSource;
     }

    
Gets persistence data source name in JNDI.

Returns:
Persistence data source name in JNDI.
 
     protected String getDataSourceName() {
         return ;
     }

    
Do some activity within DB connection.

Parameters:
action Unit of work to do within connection.
<T> Action result type.
Returns:
Action result value.
Throws:
java.io.IOException In case of any IO exception.
 
     protected <T> T doWithinConnection(Action<T> actionthrows IOException {
         Connection connection = null;
 
         try {
             connection = getDataSource().getConnection();
             connection.setAutoCommit(false);
 
             T result = action.execute(connection);
 
             connection.commit();
             connection.close();
             connection = null;
 
             return result;
         }
         catch (SQLException e) {
             throw new IOException("Failed to execute SQL request."e);
        }
        finally {
            closeConnection(connection);
        }
    }

    
Close DB connection.

Parameters:
connection DB connection to close.
    private void closeConnection(Connection connection) {
        if (connection == null)
            return;
        try {
            connection.rollback();
        }
        catch (Throwable t) {
            .info("Failed to rollback connection."t);
        }
        try {
            connection.close();
        }
        catch (Throwable t) {
            .info("Failed to close connection."t);
        }
    }

    
Action to perform within DB connection.

Parameters:
<T> Action result type.
    protected interface Action<T> {
        
Unit of work to perform within DB connection.

Parameters:
connection DB connection to work with.
Returns:
Operation result.
Throws:
java.io.IOException Exception to throw on any IO error.
java.sql.SQLException Exception to throw on any SQL error.
        T execute(Connection connectionthrows SQLExceptionIOException;
    }
New to GrepCode? Check out our FAQ X