Spontaneously generated gravity and the second law of thermodynamics