This paper is concerned with certain phase transition phenomena arising in a family of stochastic ergodic control problems having real parameter b. We show that the large time behavior of the optimal diffusion changes drastically in the vicinity of some critical value b ¼ b c. Specifically, the optimal diffusion is recurrent for b\b c , while it is transient for b [ b c. We also investigate the large time behavior of the optimal diffusion for b ¼ b c which turns out to be different from the previous two cases and more subtle. Our proof is based on the Lyapunov method giving analytical criteria for recurrence and transience of diffusions. The key lies in the analysis of solutions to the associated viscous Hamilton-Jacobi equation with bounded inward drift. In particular, a refined version of the gradient estimate for solutions to viscous Hamilton-Jacobi equations plays a substantial role. Keywords Stochastic ergodic control Á Viscous Hamilton-Jacobi equation Á Generalized principal eigenvalue Á Recurrence and transience Mathematics Subject Classification 35Q93 Á 60J60 Á 93E20 This article is part of the section ''Viscosity solutions-Dedicated to Hitoshi Ishii on the award of the 1st Kodaira Kunihiko Prize'' edited by Kazuhiro Ishige, Shigeaki Koike, Tohru Ozawa, Senjo Shimizu.