I had the very same problem for MONTHS and it was driving me crazy. MacBook 15" Early 2011, 512MB SSD + 2TB HDD instead of the optical drive. The HDD kept ejecting randomly, requiring switching the MacBook completely off and back on. I thought it's a faulty drive, cables, or another piece of hardware. I even thought of buying a new MacBook.
Then I googled out a solution: try swapping the two internal drives... Now I have the startup SSD in place of the original DVD drive and my classic 2TB HDD in place of the original HDD. No random eject since then, for 2 weeks or so, already.
Can't find the original solution, but the explanation had something to do with SATA 2 vs SATA 3, etc. So I am trying to pass this solution to others that came across the same problem.
P.S. you don't need to change any settings, your system will boot just fine after swapping the two drives.