MySQL NDB Cluster + Hibernate
Does anyone know开发者_Go百科 if you can use mySQL Cluster with Hibernate?
First of all, an important part of the question is the JDBC driver support and it appears that you can use JDBC (Connector/J) with MySQL Cluster (see this blog post) so one can imagine declaring such a JDBC URL in Hibernate's configuration.
Then, my understanding of the MySQL Cluster Overview is that data nodes are seen as a whole (from the documentation, if one application updates the salary of an employee, all other MySQL servers that query this data can see this change immediately) so Hibernate should be happy with that.
However, if you want to have Hibernate create tables using the NDB engine, you'll need a special dialect (see HHH-1496). Nothing complicated though.
So in theory, everything seems ok. In practice, I don't have any experience with such a setup.
We use MySQL NDB with Hibernate and it works without any adapation in Hibernate and we did not observe consistency problems. NDB does however behave differently compared to say InnoDB, in particular with large data sets. Indexes have to fit into memory, it is rather sensible when it comes to long running transactions, and there is limits on how many rows you can lock (you can tweak those, but still a harmless looking "delete from T where x<5" may run or fail depending on how many rows need to locked). So, it is not Hibernate but the SQL created by Hibernate that may not work as you expected on NDB.
Extend MySQL5Dialect. This is an example:
public class CustomMySQL5Dialect extends MySQL5Dialect {
public String getTableTypeString() {
return " engine=ndb";
}
}
Certain features of Hibernate will absolutely not work with MySQL NDB clusters simply due to limitations on the MySQL database/driver side of things. One off the top of my head is that Hibernate uses temporary tables to implement bulk updates/deletes against multi-table structures (secondary tables, joined-subclass, etc). But MySQL NDB does not support temporary tables. Have a look at the MySQL NDB limitations page for more specific NDB limitations and whether they might affect you: http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-limitations.html
To help the next readers, we have been using NDBCluster with Hibernate in our high performance applications (support thousands of concurrent users) for over 4 years now. Note that prior to version 7.3.1, MySQL NDB doesn't support foreign key constraints (http://dev.mysql.com/doc/relnotes/mysql-cluster/7.3/en/mysql-cluster-news-5-6-10-ndb-7-3-1.html). Hence we had to write a little utility to auto-inject necessary triggers (see http://dev.mysql.com/tech-resources/articles/mysql-enforcing-foreign-keys.html).
NDB older than 7.3.1, you can use something like this:
public class MySQL5NDBDialect extends MySQLDialect
{
private static final String ENGINE_NDB = " ENGINE=NDB"; //$NON-NLS-1$
@Override
public boolean supportsCascadeDelete()
{
return false;
}
@Override
public boolean dropConstraints()
{
return false;
}
@Override
public String getTableTypeString()
{
return ENGINE_NDB;
}
@Override
public String getAddForeignKeyConstraintString(final String constraintName, final String[] foreignKey,
final String referencedTable, final String[] primaryKey, final boolean referencesPrimaryKey)
{
// our magic to inject triggers
}
}
NDB 7.3.1 and above will be simpler:
public class MySQLNDB7Dialect extends MySQLDialect
{
private static final String ENGINE_NDB = " ENGINE=NDB"; //$NON-NLS-1$
@Override
public boolean supportsCascadeDelete()
{
return true;
}
@Override
public boolean dropConstraints()
{
return true;
}
@Override
public String getTableTypeString()
{
return ENGINE_NDB;
}
}
精彩评论