1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
3
Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
<title>com.mysql.clusterj</title>
23
<body bgcolor="white">
25
Provides classes and interfaces for using MySQL Cluster directly from Java.
27
This package contains three main groups of classes and interfaces:
29
<li>A class for bootstrapping
30
<li>Interfaces for use in application programs
31
<li>Classes to define exceptions
34
<h3>Major Interfaces</h3>
35
ClusterJ provides these major interfaces for use by application programs:
36
{@link com.mysql.clusterj.SessionFactory},
37
{@link com.mysql.clusterj.Session},
38
{@link com.mysql.clusterj.Transaction},
39
{@link com.mysql.clusterj.query.QueryBuilder},
41
{@link com.mysql.clusterj.Query}.
43
<h3>Bootstrapping</h3>
45
The helper class {@link com.mysql.clusterj.ClusterJHelper} contains methods
46
for creating the {@link com.mysql.clusterj.SessionFactory}.
47
<em>Bootstrapping</em> is the process of identifying a MySQL Cluster and
48
obtaining the SessionFactory for use with the cluster. There is one
49
SessionFactory per cluster per Java VM.
51
<h3>SessionFactory</h3>
53
The {@link com.mysql.clusterj.SessionFactory} is configured via properties, which identify the
54
MySQL Cluster that the application connects to:
56
<li>com.mysql.clusterj.connectstring identifies the ndb_mgmd host name and port</li>
57
<li>com.mysql.clusterj.connect.retries is the number of retries when connecting</li>
58
<li>com.mysql.clusterj.connect.delay is the delay in seconds between connection retries</li>
59
<li>com.mysql.clusterj.connect.verbose tells whether to display a message
60
to System.out while connecting</li>
61
<li>com.mysql.clusterj.connect.timeout.before is the number of seconds to wait
62
until the first node responds to a connect request</li>
63
<li>com.mysql.clusterj.connect.timeout.after is the number of seconds to wait
64
until the last node responds to a connect request</li>
65
<li>com.mysql.clusterj.connect.database is the name of the database to use</li>
69
File propsFile = new File("clusterj.properties");
70
InputStream inStream = new FileInputStream(propsFile);
71
Properties props = new Properties();
73
SessionFactory sessionFactory = ClusterJHelper.getSessionFactory(props);
78
The {@link com.mysql.clusterj.Session} represents the user's individual connection to
79
the cluster. It contains methods for:
81
<li>finding persistent instances by primary key</li>
82
<li>persistent instance factory (newInstance)</li>
83
<li>persistent instance life cycle management (persist, remove)</li>
84
<li>getting the QueryBuilder</li>
85
<li>getting the Transaction (currentTransaction)</li>
88
Session session = sessionFactory.getSession();
89
Employee existing = session.find(Employee.class, 1);
90
if (existing != null) {
91
session.remove(existing);
93
Employee newemp = session.newInstance(Employee.class);
94
newemp.initialize(2, "Craig", 15, 146000.00);
95
session.persist(newemp);
100
The {@link com.mysql.clusterj.Transaction} allows users to combine multiple operations
101
into a single database transaction. It contains methods to:
103
<li>begin a unit of work</li>
104
<li>commit changes from a unit of work</li>
105
<li>roll back all changes made since the unit of work was begun</li>
106
<li>mark a unit of work for rollback only</li>
107
<li>get the rollback status of the current unit of work</li>
110
Transaction tx = session.currentTransaction();
112
Employee existing = session.find(Employee.class, 1);
113
Employee newemp = session.newInstance(Employee.class);
114
newemp.initialize(2, "Craig", 146000.00);
115
session.persist(newemp);
119
<h3>QueryBuilder</h3>
121
The {@link com.mysql.clusterj.query.QueryBuilder} allows users to build queries.
122
It contains methods to:
124
<li>define the Domain Object Model to query</li>
125
<li>compare properties with parameters using:
131
<li>greaterEqual</li>
136
<li>combine comparisons using "and", "or", and "not" operators</li>
139
QueryBuilder builder = session.getQueryBuilder();
140
QueryDomainType<Employee> qemp = builder.createQueryDefinition(Employee.class);
141
Predicate service = qemp.get("yearsOfService").greaterThan(qemp.param("service"));
142
Predicate salary = qemp.get("salary").lessEqual(qemp.param("salaryCap"));
143
qemp.where(service.and(salary));
144
Query<Employee> query = session.createQuery(qemp);
145
query.setParameter("service", 10);
146
query.setParameter("salaryCap", 180000.00);
147
List<Employee> results = query.getResultList();