So, I'm want to do this:
package apache.ignite.schemas;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.cache.Cache;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStore;
import org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.transactions.Transaction;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class DemoDemo {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/PERSON";
static final String USER = "root";
static final String PASS = "mysql";
/**
* Constructs and returns a fully configured instance of a
CacheJdbcPojoStoreFactory .
*/
private static class MySQLDemoStoreFactory<K, V> extends
CacheJdbcPojoStoreFactory<K, V> {
//{@inheritDoc}
@Override public CacheJdbcPojoStore<K, V> create() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setURL("jdbc:mysql://localhost/PERSON");
dataSource.setUser("root");
dataSource.setPassword("mysql");
setDataSource(dataSource);
return super.create();
}
}
/**
* Executes demo.
*/
public static void main(String[] args) throws IgniteException {
System.out.println(">>> Start demo...");
Connection conn = null;
Statement stmt = null;
//This block just tests that the database is set up correctly and is
able to get/display the values from the table
// Register JDBC driver
try {
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn =
DriverManager.getConnection(DB_URL,USER,PASS);
// Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, first_name, last_name FROM
PERSON";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
//Display values
System.out.print("ID: " + id);
System.out.print(", First: " + firstName);
System.out.println(", Last: " + lastName);
}
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
// Start Ignite node.
try (Ignite ignite =
Ignition.start("examples/config/example-ignite.xml")) {
// Configure cache store.
CacheConfiguration<PersonKey, Person> cfg =
CacheConfig.cache("PersonCache", new
MySQLDemoStoreFactory<PersonKey, Person>());
try (IgniteCache<PersonKey, Person> cache =
ignite.getOrCreateCache(cfg)) {
// Preload cache from database.
preload(cache);
// Read-through from database
// and store in cache.
readThrough(cache);
// Perform transaction and
// write-through to database.
transaction(ignite, cache);
}
}
}
/**
* Demonstrates cache preload from database.
*/
private static void preload(IgniteCache<PersonKey, Person> cache) {
System.out.println();
System.out.println(">>> Loading entries from database.");
// Preload all person keys that are less than or equal to 3.
cache.loadCache(null, PersonKey.class.getName(), "select * from
PERSON where ID <= 3");
for (Cache.Entry<PersonKey, Person> person : cache)
System.out.println(">>> Loaded Person: " + person);
}
/**
* Demonstrates cache read through from database.
*/
private static void readThrough(IgniteCache<PersonKey, Person> cache) {
PersonKey key = new PersonKey(4);
System.out.println();
System.out.println(">>> Read-through person from database for ID: "
+ key.getId());
// Check that person with ID=4 is not in cache.
Person p = cache.localPeek(key);
assert p == null;
// Read-through from database and store into cache.
p = cache.get(new PersonKey(4));
System.out.println(">>> Loaded person from database: " + p);
}
/**
* Demonstrates cache transaction joining database transaction.
*/
private static void transaction(Ignite ignite, IgniteCache<PersonKey,
Person> cache) {
PersonKey key = new PersonKey(5);
System.out.println();
System.out.println(">>> Update salary and write-through to database
for person with ID: " + key.getId());
try (Transaction tx = ignite.transactions().txStart()) {
// Read-through from database.
Person p = cache.get(key);
System.out.println(">>> Loaded person from database: " + p);
double salary = p.getSalary();
// Raise salary by 20%.
p.setSalary(salary * 1.2);
// Write-through to database
// and store in cache.
cache.put(key, p);
System.out.println("Key: " + key + " Value: " + p + " put in
cache");
try{
System.out.println("Executing another client");
runProcess("javac -cp gridgain-examples
C:/Users/Desktop/gridgain/examples/src/main/java/apache/ignite/schemas/Test.java");
System.out.println("******");
runProcess("java -cp gridgain-examples
C:/Users/Desktop/gridgain/examples/src/main/java/apache/ignite/schemas/Test.java");
} catch(Exception e) {
e.printStackTrace();
}
tx.commit();
}
System.out.println(">>> Updated person: " + cache.get(key));
}
private static void printLines(String cmd, InputStream ins) throws
Exception {
String line = null;
BufferedReader in = new BufferedReader(
new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(cmd + " " + line);
}
}
private static void runProcess(String command) throws Exception {
Process pro = Runtime.getRuntime().exec(command);
printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
pro.waitFor();
System.out.println(command + " exitValue() " + pro.exitValue());
}
}
in the transaction method, before tx.commit(), I want to execute another
java program called Test.java, which is as follows:
package apache.ignite.schemas;
import javax.cache.Cache;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStore;
import org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory;
import org.apache.ignite.configuration.CacheConfiguration;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class Test {
private static class MySQLDemoStoreFactory<K, V> extends
CacheJdbcPojoStoreFactory<K, V> {
//{@inheritDoc}
@Override public CacheJdbcPojoStore<K, V> create() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setURL("jdbc:mysql://localhost/PERSON");
dataSource.setUser("root");
dataSource.setPassword("mysql");
setDataSource(dataSource);
return super.create();
}
}
public static void main(String[] args) throws IgniteException {
System.out.println("Inside Test.java");
try (Ignite ignite =
Ignition.start("examples/config/example-ignite.xml")) {
// Configure cache store.
CacheConfiguration<PersonKey, Person> cfg =
CacheConfig.cache("PersonCache", new
MySQLDemoStoreFactory<PersonKey, Person>());
try (IgniteCache<PersonKey, Person> cache =
ignite.getOrCreateCache(cfg)) {
test(cache);
}
}
}
public static void test(IgniteCache<PersonKey, Person> cache)
{
for (Cache.Entry<PersonKey, Person> person : cache)
System.out.println(">>> Loaded Person: " + person);
}
}
But, it isn't executing. Please tell me how do I do it?
--
View this message in context:
http://apache-ignite-users.70518.x6.nabble.com/How-to-perform-lazy-write-to-database-tp4002p4027.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.