Why do dogs lock while they are breeding?

he has glands at the base of his penis that the female grips onto whilst mating