Switching between Stage & Production DB with Tapestry5 & Hibernate3

  • create a hibernate.cfg.xml like this sample and put it into your classpath

    <hibernate-configuration>  
      <session-factory>   
        <property name="hibernate.connection.provider_class">
           org.hibernate.connection.C3P0ConnectionProvider
        </property>
        <property name="hibernate.c3p0.max_size">5</property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.timeout">5000</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.validate">false</property>
        <property name="hibernate.cache.provider_class">
          org.hibernate.cache.EhCacheProvider
        </property>
        <property name="hibernate.cache.provider_configuration_file_resource_path">
          /ehcache.xml
        </property>
        <property name="hibernate.cache.use_query_cache">true</property>
        <property name="hibernate.current_session_context_class">
           org.hibernate.context.ThreadLocalSessionContext
        </property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.generate_statistics">true</property>
        <property name="hibernate.cache.use_structured_entries">true</property>
        <property name="hibernate.jdbc.fetch_size">5</property>
        <property name="hibernate.jdbc.batch_size">5</property>
        <property name="hibernate.max_fetch_depth">3</property>
        <property name="hibernate.default_batch_fetch_size">8</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        </session-factory>
    </hibernate-configuration>
    
  • create a file called "hibernate.properties" somewhere in your local filesystem
    hibernate.connection.url = jdbc:mysql://<jdbc_url>
    hibernate.connection.driver_class = com.mysql.jdbc.Driver
    hibernate.connection.username = <username>
    hibernate.connection.password = <password>
    hibernate.dialect = de.hsofttec.services.utils.MySQL5InnoDBDialect
    hibernate.connection.autocommit = false
    
  • create your own HibernateConfigurer class
    public class MyHibernateConfigurer implements HibernateConfigurer
    {
     private final Properties extraProperties;
    
     public MyHibernateConfigurer(Properties extraProperties)
     {
      this.extraProperties = extraProperties;
     }
    
     public void configure(Configuration configuration)
     {
      configuration.addProperties(extraProperties);
      configuration.configure();
     }
    }
    
  • put this code into you application module (IOC)
    public static void bind(ServiceBinder binder)
     {
      binder.bind(ShipmentAdviceDAO.class, ShipmentAdviceDAOHibernate.class);
      binder.bind(ShipmentAdviceItemDAO.class, ShipmentAdviceItemDAOHibernate.class);
      binder.bind(ShipmentOrderDAO.class, ShipmentOrderDAOHibernate.class);
      binder.bind(ShipmentParcelDAO.class, ShipmentParcelDAOHibernate.class);
      binder.bind(ShipmentUnitTypeDAO.class, ShipmentUnitTypeDAOHibernate.class);
      binder.bind(ShipmentTypeDAO.class, ShipmentTypeDAOHibernate.class);
     }
    
     /**
      * add entities package to hibernate configuration.
      */
     public void contributeHibernateEntityPackageManager(
    Configuration configuration)
     {
      configuration.add("de.hsofttec.sts.entities");
     }
    
     public static HibernateConfigurer buildMyHibernateConfigurer(
    @Inject @Symbol("appl.config.dir") String applConfigDir)
     {
      Resource resource;
      Properties properties = new Properties();
    
      try
      {
       String fileName = applConfigDir + "/hibernate.properties";
       File file = new File(fileName);
       if (!file.canRead())
        throw new RuntimeException(String.format("can't read file '%s'", file.toURI()));
    
       resource = new URIResource(file.toURI());
       properties.load(resource.openStream());
      }
      catch (MalformedURLException e)
      {
       throw new RuntimeException(e);
      }
      catch (IOException e)
      {
       throw new RuntimeException(e);
      }
    
      return new MyHibernateConfigurer(properties);
     }
    
     public static void contributeHibernateSessionSource(
    OrderedConfiguration config,
    @InjectService("MyHibernateConfigurer") HibernateConfigurer hibernateConfigurer)
     {
      config.add("Application", hibernateConfigurer);
     }
    
  • and start your application or servlet container with the system property
    "appl.config.dir" that points to the directory where the file "hibernate.properties"
    resists.

Kommentare