Ex-NBA ref Tim Donaghy tells TMZ Sports he owes his life to the leader of a white prison gang ... saying he would've been murdered behind bars if not for their protection.
Donaghy was infamously sentenced to 15 months of hard time back in '08 for his role in an NBA gambling scandal. While locked up, the ex-ref says he was brutally attacked by a fellow inmate.
"I was a target. There was a guy who took a paint roller extension pole and blasted me in the knee a few times," Donaghy tells TMZ Sports. "I had to have surgery to relieve the pain when I got out of prison."
The Donaghy says after the attack, he needed to find protection if he was going to survive his stint ... so he started to hang out with the only group that offered to have his back.
"Whites run together, blacks run together, Mexicans, Puerto Ricans, Italians."
Donaghy says the white gang wanted to initiate him with a prison tattoo -- involving a pin and ink from a pen -- but he managed to talk the guys into "option #2" ... a shaved head.
"It was a matter of me surviving, and getting the f**k outta there" ... Donaghy says.
T.D. says he owes his life to one of the leaders ... a guy nicknamed "St. Pete" ... "Thank God I teamed up with him. He saved my life. There's no doubt about it, I never would've survived if it weren't for St. Pete."
They've even maintained a relationship after prison ... "We've stayed in touch. Every now and then we'll talk on the phone. We even got lunch once."
Who says jail friendships don't last?