From the spec, it didn't mention that the user define DataSet needs to set with @ResultColumn(uniqueIdentifier=true) in order for data to be change when DataSetResolver.sync() is called. The result is different between with @ResultColumn set and without setting it.
I think the correct output should be Output #1, because when there is a conflited row, I can change the data to be same as the DataSet or same as the DataSouce. The sample program is trying to change the data as same as the DataSet. It did show the DataSource had change from final query.
As of Output #2, the data from the DataSource didn't change after DataSetResolver.sync called. It still has the same data as before.
How to run it
==============
change the DB IP, username and passed.
#1
1) javac QueryTest.java
2) java QueryTest
#2
1) PersonDo.java
change @ResultColumn(uniqueIdentifier=true)public long id ;
to public long id
2) javac QueryTest.java
3) java QueryTest
Output
======
#1
Calling query.getAllPersons() ...
DataSet.size=2
Id: 1 First Name: DataSet_syncproblemFirst Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
Updated the result set row
Caught : javax.sql.rowset.spi.SyncProviderException: 2 conflicts while synchronizing
pd id: 1
pd firstName: DataSet_syncproblemFirst
pd lastName: last_name1
After syncing....
Id: 1 First Name: DataSet_syncproblemFirst Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
#2
Calling query.getAllPersons() ...
DataSet.size=2
Id: 1 First Name: DataSet_syncproblemFirst Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
Updated the result set row
Caught : javax.sql.rowset.spi.SyncProviderException: 2 conflicts while synchronizing
pd id: 1
pd firstName: DataSet_syncproblemFirst
pd lastName: last_name1
After syncing....
Id: 1 First Name: ResultSet_thiiswrong Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
Sample Program.
================
PersonDo.java
--------------
import java.sql.ResultColumn;
public class PersonDO {
@ResultColumn(uniqueIdentifier=true)public long id ;
//public long id ;
public String firstName ;
public String lastName ;
}
Query.java
------------
import java.sql.*;
public class QueryTest {
Connection conn = null;
Connection conn1 = null;
Statement stmt = null;
private void doSetup() {
try {
Class.forName("com.inet.ora.OraDriver");
}catch (ClassNotFoundException ex) {
System.err.println("Exception when loading JDBC driver : "+ex);
}
try {
conn = DriverManager.getConnection("jdbc:inetora:IP:1521:ORCL", "user", "passed");
conn1 = DriverManager.getConnection("jdbc:inetora:IP:1521:ORCL", "user", "passed");
stmt = conn.createStatement();
stmt.executeUpdate("drop table query001");
}catch (SQLException ex) {
System.err.println("Exception : "+ex);
}
try {
stmt.addBatch("create table query001 ( id number, firstName varchar2(32), lastName varchar2(32), primary key (id))");
stmt.addBatch("insert into query001 values ( 1, 'fname1', 'last_name1')");
stmt.addBatch("insert into query001 values ( 2, 'fname2', 'last_name2')");
stmt.executeBatch();
}catch (SQLException ex) {
System.err.println("Exception when executing SQL :"+ex);
}
}
private void doTest() {
DataSet<PersonDO> rows = null;
I_Query001 query = null;
try {
query = QueryObjectFactory.createQueryObject(I_Query001.class, conn);
} catch (SQLException sqlEx) {
System.err.println("SQLException caught "+sqlEx.getMessage());
}
System.out.println("Calling query.getAllPersons() ...");
rows = query.getAllPersons();
System.out.println("DataSet.size="+rows.size());
for(PersonDO p : rows) {
if( p.id == 1)
p.firstName = "DataSet_syncproblemFirst";
rows.modify();
System.out.println("Id: "+p.id+" First Name: "+p.firstName+" Last Name: "+p.lastName);
}
// Here change the value in the DB. This should cause a sync problem.
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select * from query001");
while(rs.next()) {
if(rs.getInt(1) == 1) {
rs.updateString(2,"ResultSet_thiiswrong");
rs.updateRow();
System.out.println("Updated the result set row");
}
}
} catch(Exception e) {
System.out.println("Unexpected Exception: "+e.getMessage());
}
try
{
rows.sync();
} catch(SQLDataSetSyncException spe) {
System.out.println("Caught : "+spe.getMessage());
DataSetResolver<ConflictingRow> ds = spe.getDataSetResolver();
for(ConflictingRow cr : ds) {
PersonDO pd = (PersonDO)cr.getRow();
System.out.println("pd id: "+pd.id);
System.out.println("pd firstName: "+pd.firstName);
System.out.println("pd lastName: "+pd.lastName);
//pd.firstName = "ResultSet_thiiswrong";
pd.firstName = "DataSet_syncproblemFirst";
}
ds.sync();
}
rows = query.getAllPersons();
System.out.println("After syncing....");
for(PersonDO p : rows) {
System.out.println("Id: "+p.id+" First Name: "+p.firstName+" Last Name: "+p.lastName);
}
/*
System.out.println("Modify again ....");
for(PersonDO p : rows) {
if( p.id == 1)
p.firstName = "ModifyAgainFirst";
rows.modify();
System.out.println("Id: "+p.id+" First Name: "+p.firstName+" Last Name: "+p.lastName);
}
try
{
rows.sync();
} catch(SQLDataSetSyncException spe) {
System.out.println ("SQLDataSetSyncExc again");
}*/
}
public static void main(String[] args) {
QueryTest qtest = new QueryTest();
qtest.doSetup();
qtest.doTest();
}
}
interface I_Query001 extends BaseQuery {
@Select(sql="SELECT id , firstName, lastName from query001",connected=false,tableName="query001",readOnly=false)
DataSet<PersonDO> getAllPersons() ;
}
I think the correct output should be Output #1, because when there is a conflited row, I can change the data to be same as the DataSet or same as the DataSouce. The sample program is trying to change the data as same as the DataSet. It did show the DataSource had change from final query.
As of Output #2, the data from the DataSource didn't change after DataSetResolver.sync called. It still has the same data as before.
How to run it
==============
change the DB IP, username and passed.
#1
1) javac QueryTest.java
2) java QueryTest
#2
1) PersonDo.java
change @ResultColumn(uniqueIdentifier=true)public long id ;
to public long id
2) javac QueryTest.java
3) java QueryTest
Output
======
#1
Calling query.getAllPersons() ...
DataSet.size=2
Id: 1 First Name: DataSet_syncproblemFirst Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
Updated the result set row
Caught : javax.sql.rowset.spi.SyncProviderException: 2 conflicts while synchronizing
pd id: 1
pd firstName: DataSet_syncproblemFirst
pd lastName: last_name1
After syncing....
Id: 1 First Name: DataSet_syncproblemFirst Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
#2
Calling query.getAllPersons() ...
DataSet.size=2
Id: 1 First Name: DataSet_syncproblemFirst Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
Updated the result set row
Caught : javax.sql.rowset.spi.SyncProviderException: 2 conflicts while synchronizing
pd id: 1
pd firstName: DataSet_syncproblemFirst
pd lastName: last_name1
After syncing....
Id: 1 First Name: ResultSet_thiiswrong Last Name: last_name1
Id: 2 First Name: fname2 Last Name: last_name2
Sample Program.
================
PersonDo.java
--------------
import java.sql.ResultColumn;
public class PersonDO {
@ResultColumn(uniqueIdentifier=true)public long id ;
//public long id ;
public String firstName ;
public String lastName ;
}
Query.java
------------
import java.sql.*;
public class QueryTest {
Connection conn = null;
Connection conn1 = null;
Statement stmt = null;
private void doSetup() {
try {
Class.forName("com.inet.ora.OraDriver");
}catch (ClassNotFoundException ex) {
System.err.println("Exception when loading JDBC driver : "+ex);
}
try {
conn = DriverManager.getConnection("jdbc:inetora:IP:1521:ORCL", "user", "passed");
conn1 = DriverManager.getConnection("jdbc:inetora:IP:1521:ORCL", "user", "passed");
stmt = conn.createStatement();
stmt.executeUpdate("drop table query001");
}catch (SQLException ex) {
System.err.println("Exception : "+ex);
}
try {
stmt.addBatch("create table query001 ( id number, firstName varchar2(32), lastName varchar2(32), primary key (id))");
stmt.addBatch("insert into query001 values ( 1, 'fname1', 'last_name1')");
stmt.addBatch("insert into query001 values ( 2, 'fname2', 'last_name2')");
stmt.executeBatch();
}catch (SQLException ex) {
System.err.println("Exception when executing SQL :"+ex);
}
}
private void doTest() {
DataSet<PersonDO> rows = null;
I_Query001 query = null;
try {
query = QueryObjectFactory.createQueryObject(I_Query001.class, conn);
} catch (SQLException sqlEx) {
System.err.println("SQLException caught "+sqlEx.getMessage());
}
System.out.println("Calling query.getAllPersons() ...");
rows = query.getAllPersons();
System.out.println("DataSet.size="+rows.size());
for(PersonDO p : rows) {
if( p.id == 1)
p.firstName = "DataSet_syncproblemFirst";
rows.modify();
System.out.println("Id: "+p.id+" First Name: "+p.firstName+" Last Name: "+p.lastName);
}
// Here change the value in the DB. This should cause a sync problem.
try {
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select * from query001");
while(rs.next()) {
if(rs.getInt(1) == 1) {
rs.updateString(2,"ResultSet_thiiswrong");
rs.updateRow();
System.out.println("Updated the result set row");
}
}
} catch(Exception e) {
System.out.println("Unexpected Exception: "+e.getMessage());
}
try
{
rows.sync();
} catch(SQLDataSetSyncException spe) {
System.out.println("Caught : "+spe.getMessage());
DataSetResolver<ConflictingRow> ds = spe.getDataSetResolver();
for(ConflictingRow cr : ds) {
PersonDO pd = (PersonDO)cr.getRow();
System.out.println("pd id: "+pd.id);
System.out.println("pd firstName: "+pd.firstName);
System.out.println("pd lastName: "+pd.lastName);
//pd.firstName = "ResultSet_thiiswrong";
pd.firstName = "DataSet_syncproblemFirst";
}
ds.sync();
}
rows = query.getAllPersons();
System.out.println("After syncing....");
for(PersonDO p : rows) {
System.out.println("Id: "+p.id+" First Name: "+p.firstName+" Last Name: "+p.lastName);
}
/*
System.out.println("Modify again ....");
for(PersonDO p : rows) {
if( p.id == 1)
p.firstName = "ModifyAgainFirst";
rows.modify();
System.out.println("Id: "+p.id+" First Name: "+p.firstName+" Last Name: "+p.lastName);
}
try
{
rows.sync();
} catch(SQLDataSetSyncException spe) {
System.out.println ("SQLDataSetSyncExc again");
}*/
}
public static void main(String[] args) {
QueryTest qtest = new QueryTest();
qtest.doSetup();
qtest.doTest();
}
}
interface I_Query001 extends BaseQuery {
@Select(sql="SELECT id , firstName, lastName from query001",connected=false,tableName="query001",readOnly=false)
DataSet<PersonDO> getAllPersons() ;
}