I need to communicate with database from a stateless session EJB. We are using simple JDBC (just like from any normal class) and not the EJBServer connection pools.

Table A has a primary key and table B references it for a foreign key.
Now, I am inserting a record in table A and in the same method, inserting a corresponding record in table B.

I am using PreparedStatement for the second insert (table B) and am getting the 'foreign key constraint violated' error, even though the required primary key value is now present in table A.

Interestingly, if I use Statement now, and change nothing else, it works!

I would be grateful if someone could throw any light on this.

Note: Statement and PreparedStatement both are working from simple java class.