Early in the trial, Judge Walls made it clear that he was not going to let any of the salacious rumors work their way into his courtroom, nor what he viewed as loaded references to Mr. Menendez’s decisions to stay in luxury hotels with a female friend.

“It’s not going to be a tabloid trial, and I am not going to let you just swish and swash nonsensical, you know, nonsensical substances — not substances, scenarios that really don’t even make for a good pulp fiction story,” he told a prosecutor at one point.

Gerald Krovatin, a lawyer in Newark who had worked with Mr. Menendez’s defense team, said, “They had really criminalized a relationship. It had its seamy underside with the Melgen girlfriends, but it was kind of a bloodless case once Walls cut all the sex out of it.”

Federal prosecutors saw their case slip away. The jury was deadlocked. Judge Walls tried to encourage them to reach a decision but eventually had to declare a mistrial. Jurors told reporters afterward that they were stuck at 10-2, with the majority for acquittal. It was unusual insight into a jury’s deliberations, and it made some in the Justice Department unenthusiastic about a retrial.

The government was facing other hurdles.

Judge Walls had said he would not preside over the retrial, and the case was expected to go to Judge William Martini. Reflecting Mr. Menendez’s long and deep involvement in New Jersey politics, every other federal judge in Newark, where the first trial was held, had either passed or recused from the case; Mr. Menendez had helped sponsor several of the judges for the bench.

Prosecutors had reason to feel anxious about arguing the case before Judge Martini. A former Republican congressman — he served in the House with Mr. Menendez in the mid 1990s — he has been an outspoken critic of government overreach.