views:

88

answers:

2

Hi, I have 2 tables named T1 and T2. Where T1 is parent and T2 is child. The scenario is, I started a jdbc transaction and then insert a row in T1 and then try to insert a row in T2. Inserting row in T2 gies me "Integrity Constraint-Parent key not found" exception.

How i handle this scenario ?

 Connection con;
try{
  con = ConnectionPool.getConnection();
  con.setAutoCommit(false);
  int T1Id = getNewId("T1"); // from sequence;
  int T2Id = getNewId("T2"); // from sequence;
  Insert in to table T1(t1Id,tName) values (T1Id,'A')
  Insert in to table T2(t2Id, t1Id,tName) values (T2Id,T1Id,'A')//Here, Exception raises

  con.commit();

 }catch(Exception e){
    try {con.rollback();} catch (SQLException e) {}

 }finally{
     try {con.setAutoCommit(true);} catch (SQLException e) {}
     ConnectionPool.returnConnection(con);
}

Using JDBC API, struts1.2, Oracle10 G Database

+1  A: 

You are probably doing something wrong. If both inserts are within the same transaction what you've just mentioned can't happen. Please share some code and more information (DB server, table structures) to see if we can help you.

Pablo Santa Cruz
did you understand my question ?
Zeeshan
Yes, I did. And I am telling you that it can't happen if you do both inserts within the same transaction. We still need more info to help you.
Pablo Santa Cruz
My friend, now here is the actual code
Zeeshan
A: 

You need a three step process:

  1. INSERT row into parent
  2. SELECT the generated key from the parent
  3. Use the generated key and the child information to INSERT into the child

It should be a single unit of work, so make it transactional.

It's impossible to tell from your pseudo code. It'd also be helpful to know whether or not you're using auto generated keys.

I'm guessing that the primary key you're assuming for T1 doesn't actually appear. If T2 says the foreign key cannot be null or is required, and it doesn't appear in T1, then the RDBMS system should complain and throw an exception.

duffymo
I am doing step 1 and 3. Skip step2 because i already have newly generated id, got from sequence. All 3 steps are already in a transaction.
Zeeshan
"m guessing that the primary key you're assuming for T1 doesn't actually appear. If T2 says the foreign key cannot be null or is required, and it doesn't appear in T1, then the RDBMS system should complain and throw an exception." Yes this is scenario, primarykey for T1 doesn't appears becuase of transaction.
Zeeshan
You need to either generate a key that you can INSERT into both OR do the equivalent of a flush without committing.
duffymo
T1 is parent table and i have T1's primary key. Then i use this key int T2's table where T1's key as foreign key. Now when i execute T2's insert SQL then the "Parent keynot found" exception raises and transaction is rolled back.
Zeeshan
Yes, so you've said three times now. There's no new information here. Can't help you if you just keep repeating the same story over and again.
duffymo
That's not Java code. Still pseudo code. I'll vote to close this.
duffymo