Primary Key Conflicts While Inserting Rows

Hi,

I am inserting some rows into a table which has primary key constraints on columns 1,2,3. I have set the key columns in my program to 1,2,3. When I try to insert a row it throws a syncresolver exception stating that there is a key conflict. But i don't understand why it is doing this because there is no conflict. I can remove the primary key constraints off the sql table (but still leave them on in the program) and it will insert everything correctly.

Any ideas?

Thanks

[503 byte] By [bce_developera] at [2007-11-27 11:07:43]
# 1

If the database is telling you there is a key conflict and you are saying there isn't, then my money would be on the database being correct and you being wrong.

DrClapa at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

If you have 3 primary keys, your database may be set up to either require all 3 keys together be unique, or each by themselves to be unique. My guess is the second case.

If you search on google for

syncresolver exception sql

you may get more information to help you, such as:

http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/spi/SyncProviderException.html

George123a at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

I cleared the table completely and ran the program again. It seems to insert the first row fine but the rows after get caught by the syncProviderException handler. When it is being handled by the exception handler, it seems that the first column is causing the conflict as if it thinks that column 1 is the only primary key but there are 3 keys on the table. I tried removing the primary key on column 1 and now it inserts the rows but has conflicts when there is a duplicate match on column 2 -- ie. it ignores the PK on column 3. Am i missing something in the configuration of the PKs? I have specified the keys in setKeyColumns to {1,2,3}. The database is Microsoft SQL Server 2000 and I am using JTDS JDBC Drivers. May I did not correctly setup the PKs on the DB?

bce_developera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

George, would you happen to know how I can set up the MS SQL DB such that the 3 keys are unique together?

bce_developera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Sorry, I never worked much with MS SQL DB. I'm out of ideas.

George123a at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

I am almost certain that Java is interpreting the PKs individually whereas the DB interprets them together. I've tried manually adding rows into the DB and SQL does not throw an error. However, inserting the same rows through Java does throw an error. Am I not setting something correctly in Java?

bce_developera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

I don't think you're doing what you think you're doing. But it's silly for us to try and second guess you.

Create an example table and a simple Java app to demonstrate what you're trying to do, then post the DDL and the code.

dcmintera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

Using SQL Server 2005 Drivers.

import java.sql.*;

import javax.sql.rowset.*;

import com.sun.rowset.*;

public class TestDB {

public static void main(String[] args){

String connectionUrl = "jdbc:sqlserver://DBSERVER;DatabaseName=fTrack;user=sa;password=1234";

// Declare the JDBC objects.

Connection con = null;

CachedRowSet crs = null;

int keys[] = {1,2,3};

try {

// Establish the connection.

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

con = DriverManager.getConnection(connectionUrl);

// Create and execute an SQL statement that returns some data.

String SQL = "SELECT * FROM pt_OMS_Data WHERE Switch=4";

crs = new CachedRowSetImpl();

crs.setCommand(SQL);

crs.execute(con);

crs.setKeyColumns(keys);

crs.beforeFirst();

crs.moveToInsertRow();

crs.updateInt(1, 5);

crs.updateTimestamp(2, Timestamp.valueOf("2007-07-20 00:00:00"));

crs.updateInt(3, 0);

crs.insertRow();

crs.moveToCurrentRow();

crs.moveToInsertRow();

crs.updateInt(1, 5);

crs.updateTimestamp(2, Timestamp.valueOf("2007-07-20 00:00:00"));

crs.updateInt(3, 1);

crs.insertRow();

crs.moveToCurrentRow();

crs.acceptChanges(con);

}

// Handle any errors that may have occurred.

catch (Exception e) {

e.printStackTrace();

} finally {

if (crs != null)

try {

crs.close();

} catch (Exception e) {

}

if (con != null)

try {

con.close();

} catch (Exception e) {

}

}

}

}

Layout of DB:

Switch Date BSC ColA ColB ColC

Switch, Date, and BSC are the PKs.

Switch is type Int, Date is type datetime, BSC is type int

Here is the error which i receive:

javax.sql.rowset.spi.SyncProviderException: 4 conflicts while synchronizing

at com.sun.rowset.internal.CachedRowSetWriter.writeData(Unknown Source)

at com.sun.rowset.CachedRowSetImpl.acceptChanges(Unknown Source)

at com.sun.rowset.CachedRowSetImpl.acceptChanges(Unknown Source)

at TestDB.main(TestDB.java:39)

I inserted the exact same data manually into the DB and it works fine.

By the way, the program inserts the first row, but not the second. Based on my trials with the original program I am working on the syncproviderexception handler seems to have a conflict on insert row on Switch column. This is why I believe java is not interpreting the PKs as a group but rather as individually.

Message was edited by:

bce_developer

bce_developera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

I tried this again using a resultset as opposed to a cachedrowset. It works with the resultset. Is there something that I am missing on my cachedrowset configuration? Can i copy all my cachedrowset data over to a resultset just before updating?

bce_developera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10

Can you post the DDL please.

dcmintera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11

I would add that using SELECT * with numbered columns isn't a great approach, though I don't think it's the cause of the problem here.

dcmintera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12

MS SQL Table:

CREATE TABLE testTable

(Switch integer,

OMTimestamp datetime,

BSC integer,

ColA integer,

ColB integer,

ColC integer

PRIMARY KEY(Switch, OMTimestamp, BSC));

bce_developera at 2007-7-29 13:23:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...