Close connections, make sure last id is defined