2 Replies Latest reply on Aug 13, 2009 4:38 AM by Galder Zamarreño

    JdbcBinaryCacheStore + JndiConnectionFactory

    John Prince Newbie

      Hi,

      This might be fixed with the patch for the FileCacheStore, but just in case... The VersionAwareMarshaller is not started when a JdbcBinaryCacheStore is configured, so a NPE comes from the JBossMarshaller, which is also not properly initialized.

      I wrote a quick JndiConnectionFactory to get the DataSource from Jndi - it might be useful to someone else, so here it is.

      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.sql.DataSource;
      
      import java.sql.Connection;
      import java.sql.SQLException;
      
      import org.infinispan.loaders.CacheLoaderException;
      import org.infinispan.loaders.jdbc.JdbcUtil;
      import org.infinispan.loaders.jdbc.connectionfactory.ConnectionFactory;
      import org.infinispan.loaders.jdbc.connectionfactory.ConnectionFactoryConfig;
      
      /**
       * @author John Prince
       * Date: Aug 6, 2009
       * Time: 11:47:46 AM
       */
      public class JndiConnectionFactory
       extends ConnectionFactory
      {
       private DataSource dataSource;
      
       public Connection getConnection() throws CacheLoaderException
       {
       try
       {
       return dataSource.getConnection();
       }
       catch (SQLException e)
       {
       throw new CacheLoaderException("Could not get Connection from data source", e);
       }
       }
      
       public void releaseConnection(final Connection conn)
       {
       JdbcUtil.safeClose(conn);
       }
      
       public void start(final ConnectionFactoryConfig config) throws CacheLoaderException
       {
       // TODO: Abuse of connection url - new property in ConnectionFactoryConfig?
       try
       {
       // Expecting something like java:comp/env/jdbc/MyDataSource
       dataSource = (DataSource) new InitialContext().lookup(config.getConnectionUrl());
       }
       catch (NamingException e)
       {
       throw new CacheLoaderException("Problem looking up data source " + config.getConnectionUrl() + " in jndi");
       }
       if (dataSource == null)
       throw new CacheLoaderException("Could not find data source " + config.getConnectionUrl() + " in jndi");
       }
      
       public void stop()
       {
       // Nothing to do
       }
      }
      
      


      Best wishes

      John