All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.persistence.jdbc.common.JdbcUtil Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.persistence.jdbc.common;

import static org.infinispan.persistence.jdbc.common.logging.Log.PERSISTENCE;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.infinispan.commons.io.ByteBuffer;
import org.infinispan.commons.marshall.Marshaller;
import org.infinispan.commons.marshall.MarshallingException;
import org.infinispan.commons.marshall.StreamAwareMarshaller;

/**
 * Contains common methods used by JDBC CacheStores.
 *
 * @author [email protected]
 */
public class JdbcUtil {
   public static void safeClose(Statement ps) {
      if (ps != null) {
         try {
            ps.close();
         } catch (SQLException e) {
            PERSISTENCE.sqlFailureUnexpected(e);
         }
      }
   }

   public static void safeClose(Connection connection) {
      if (connection != null) {
         try {
            connection.close();
         } catch (SQLException e) {
            PERSISTENCE.sqlFailureClosingConnection(connection, e);
         }
      }
   }

   public static void safeClose(ResultSet rs) {
      if (rs != null) {
         try {
            rs.close();
         } catch (SQLException e) {
            PERSISTENCE.sqlFailureUnexpected(e);
         }
      }
   }

   public static ByteBuffer marshall(Object obj, Marshaller marshaller) {
      try {
         return marshaller.objectToBuffer(obj);
      } catch (InterruptedException e) {
         Thread.currentThread().interrupt();
         throw new MarshallingException(e);
      } catch (IOException e) {
         PERSISTENCE.errorMarshallingObject(e, obj);
         throw new MarshallingException("I/O failure while marshalling object: " + obj, e);
      }
   }

   @SuppressWarnings("unchecked")
   public static  T unmarshall(InputStream inputStream, StreamAwareMarshaller marshaller) {
      try {
         return (T) marshaller.readObject(inputStream);
      } catch (IOException e) {
         PERSISTENCE.ioErrorUnmarshalling(e);
         throw new MarshallingException("I/O error while unmarshalling from stream", e);
      } catch (ClassNotFoundException e) {
         PERSISTENCE.unexpectedClassNotFoundException(e);
         throw new MarshallingException(e);
      }
   }

   @SuppressWarnings("unchecked")
   public static  T unmarshall(ByteBuffer buf, Marshaller marshaller) {
      try {
         return (T) marshaller.objectFromByteBuffer(buf.getBuf(), buf.getOffset(), buf.getLength());
      } catch (IOException e) {
         throw new MarshallingException("I/O error while unmarshalling", e);
      } catch (ClassNotFoundException e) {
         PERSISTENCE.unexpectedClassNotFoundException(e);
         throw new MarshallingException(e);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy