31.07.2013 Views

MySQL Cluster Tutorial - cdn.oreillystatic.com

MySQL Cluster Tutorial - cdn.oreillystatic.com

MySQL Cluster Tutorial - cdn.oreillystatic.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The next step is to write the application code which we step through here block by block;<br />

the first of which simply contains the import statements and then loads the contents of the<br />

clusterj.properties defined above (Note – refer to section 3.1.1 for details on <strong>com</strong>piling and<br />

running the tutorial code):<br />

Main.java (part 1):<br />

import <strong>com</strong>.mysql.clusterj.<strong>Cluster</strong>JHelper;<br />

import <strong>com</strong>.mysql.clusterj.SessionFactory;<br />

import <strong>com</strong>.mysql.clusterj.Session;<br />

import <strong>com</strong>.mysql.clusterj.Query;<br />

import <strong>com</strong>.mysql.clusterj.query.QueryBuilder;<br />

import <strong>com</strong>.mysql.clusterj.query.QueryDomainType;<br />

import java.io.File;<br />

import java.io.InputStream;<br />

import java.io.FileInputStream;<br />

import java.io.*;<br />

import java.util.Properties;<br />

import java.util.List;<br />

public class Main {<br />

public static void main (String[] args) throws<br />

java.io.FileNotFoundException,java.io.IOException {<br />

// Load the properties from the clusterj.properties file<br />

File propsFile = new File("clusterj.properties");<br />

InputStream inStream = new FileInputStream(propsFile);<br />

Properties props = new Properties();<br />

props.load(inStream);<br />

//Used later to get userinput<br />

BufferedReader br = new BufferedReader(new<br />

InputStreamReader(System.in));<br />

The next step is to get a handle for a SessionFactory from the <strong>Cluster</strong>JHelper class and<br />

then use that factory to create a session (based on the properties imported from<br />

clusterj.properties file.<br />

Main.java (part 2):<br />

// Create a session (connection to the database)<br />

SessionFactory factory = <strong>Cluster</strong>JHelper.getSessionFactory(props);<br />

Session session = factory.getSession();<br />

Now that we have a session, it is possible to instantiate new Employee objects and then<br />

persist them to the database. Where there are no transaction begin() or <strong>com</strong>mit()<br />

statements, each operation involving the database is treated as a separate transaction.<br />

Copyright © 2010, Oracle and/or its affiliates. All rights reserved. 57/81

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!