Menu

HA JDBC with Hibernate & DataSource

Help
2013-07-09
2013-07-17
  • Syed bhai...

    Syed bhai... - 2013-07-09

    Paul,
    I am able to write basic standalone programs using ha jdbc very well.
    Next, I want to integrate ha-jdbc into hibernate? how can this be done?
    Also, I wanted to know how can I configure bone CP data source (connection pool) in ha jdbc xml file. Please help.

    Thanks,
    Syed.

     
  • Paul Ferraro

    Paul Ferraro - 2013-07-09

    Can you attach your ha-jdbc and hibernate config, so I know from where we are starting?

     
    • Syed bhai...

      Syed bhai... - 2013-07-09

      okay. Here is the ha-jdbc config file.
      //ha-jdbc-cluster.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <ha-jdbc xmlns="urn:ha-jdbc:cluster:2.1"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:schemaLocation="urn:ha-jdbc:cluster:2.1 http://ha-jdbc.github.com/xsd/ha-jdbc-2.1.0-beta-3.xsd">

      <sync id="full">
      <property name="fetchSize">1000</property>
      <property name="maxBatchSize">100</property>
      </sync>
      <state id="simple"/>
      <cluster balancer="load" dialect="mysql"
      default-sync="full" transaction-mode="parallel"
      meta-data-cache="none">
      <database id="database1" location="jdbc:mysql://localhost:3306/syedsdb">
      <user>root</user>
      <password>root</password>
      </database>
      </cluster>
      </ha-jdbc>

      //hibernate-cfg.xml
      <?xml version="1.0" encoding="utf-8"?>
      <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

      <hibernate-configuration>
      <session-factory>
      <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
      </property>
      <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
      </property>
      <property name="hibernate.connection.url">
      jdbc:mysql://localhost/test
      </property>
      <property name="hibernate.connection.username">
      root
      </property>
      <property name="hibernate.connection.password">
      root123
      </property>
      <mapping resource="Employee.hbm.xml"/>
      </session-factory>
      </hibernate-configuration>

      Waiting for your response.

       
  • Paul Ferraro

    Paul Ferraro - 2013-07-09

    Just substitute the HA-JDBC driver class name and jdbc url in your hibernate-cfg.xml.
    e.g.

    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.connection.driver_class">net.sf.hajdbc.sql.Driver</property>
            <property name="hibernate.connection.url">jdbc:ha-jdbc:cluster</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root123</property>
            <mapping resource="Employee.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    
     
    • Syed bhai...

      Syed bhai... - 2013-07-15

      Paul,
      In my project I am keeping all the configuration files outside the project. It is locating hibernate configuration file. But ha jdbc configuration file is not getting located even though both of them are in same folder. How to solve this problem? How I can tell hibernate that ha jdbc file is located in so and so folder?

      Thanks,
      Syed.

       
  • Paul Ferraro

    Paul Ferraro - 2013-07-17

    The values of any "hibernate.connection.*" properties will get passed to the call to DriverManager.getConnection(String, Properties). Consequently, you can specify the path of your ha-jdbc configuration file via.

    <property name="hibernate.connection.config">file:///path/to/ha-jdbc.xml</property>
    

    HA-JDBC will format this url using MessageFormat, allowing the name to be parameterized using the cluster identifier. e.g. file:///path/to/ha-jdbc-{0}.xml

     

Log in to post a comment.