For the duplicates you can always check first if a record exists with that combined key and if it does do nothing. I take it you are using timestamps for this and I highly doubt they are exactly the same.
For the connection issues it sounds to me that somewhere in your code you are either using persistant connections or the connection isn't properly closed,
All of this is very hard to help you with since it's using a pellet gun in the ocean hoping to hit a cod.
Provide the code for both these issues and it will be easier for us to help.