Mr. Obama had asked his White House counsel, Kathryn Ruemmler, to set up the lunch so he could build a closer rapport with the justice, according to two people briefed on the conversation. Treading cautiously, he did not directly bring up the subject of retirement to Justice Ginsburg, at 80 the Supreme Court’s oldest member and a two-time cancer patient.

He did, however, raise the looming 2014 midterm elections and how Democrats might lose control of the Senate. Implicit in that conversation was the concern motivating his lunch invitation — the possibility that if the Senate flipped, he would lose a chance to appoint a younger, liberal judge who could hold on to the seat for decades.

But the effort did not work, just as an earlier attempt by Senator Patrick Leahy, the Vermont Democrat who was then Judiciary Committee chairman, had failed. Justice Ginsburg left Mr. Obama with the clear impression that she was committed to continuing her work on the court, according to those briefed.