In the first major test of voter attitudes in 2014, Republican David Jolly won a closely watched special election for the U.S. House in Florida Tuesday, handing his party a narrow victory in a battleground district where Republicans and Democrats spent millions of dollars fine-tuning their messages on national issues ahead of the fall midterm elections.

Jolly's win in a Gulf Coast district just west of Tampa illustrated the political toxicity of the law known as Obamacare. Jolly favored repealing and replacing the law, which was a central focus of the campaign, while his Democratic opponent did not. The law's botched rollout has heightened Democrats' anxiety eight months before the midterm elections. The Florida result is likely to raise their concerns.

With Jolly holding the seat for Republicans, Democrats must pick up 17 seats to win back the House majority in the fall, a task widely viewed as extremely difficult given historical trends, President Obama's political woes and the limited pool of competitive seats up for grabs. Jolly will have to defend his seat in the fall.

As expected, the margin was close Tuesday. Jolly outpaced Democrat Alex Sink by about 3,400 votes out of 183,000. The Associated Press called the race for Jolly less than an hour after polls closed.

During the campaign, Republicans routinely ran ads tethering Sink to the health-care law, which she said should be preserved but fixed. Democrats hoped Jolly's repeal/replace posture would alienate voters and doom his chances. His victory speaks volumes about how potent a weapon the law can be for Republicans this year.

A former lobbyist, Jolly will head to Congress to succeed his old boss, late-Rep. C.W. Bill Young. Young, a moderate known for steering federal dollars to the district, served for more than four decades and was practically political royalty there. He died last fall, opening the door for Democrats in a swing district that narrowly went for Obama in 2012.

Jolly ran as a natural successor to Young. But he struggled to raise money in the campaign, lagging behind Sink, who unlike him, did not have to endure a contested primary that drained resources. Sink, Jolly and their affiliated groups spent more than $12 million in the campaign, making it one of the most expensive House races ever.

As a result of Sink's money advantage over Jolly, she enjoyed a head start out of the gates at the beginning of the general election sprint in mid-January. Voters started casting absentee ballots -- a popular way of voting in the district -- later that month, giving Democrats an opportunity to capitalize on Jolly's inability to spend big money on a positive, introductory message over the airwaves. But Sink did not build a big enough lead in absentee voting to prevail on election day.

GOP groups rallied to Jolly's side, spending some $5 million in an effort to narrow the financial gap, compared to about $3.75 million from Democratic organizations.

Libertarian Lucas Overby finished a distant third. ​

"I want to extend a big congratulations to David Jolly on his victory tonight. David proved that Pinellas County voters are tired of the devastating policies of this administration," National Republican Congressional Committee Chairman Greg Walden (Ore.) said in a statement. "Throughout this campaign, David has outlined his vision on how to grow the economy, create jobs and deliver quality healthcare for Pinellas families."

Democrats vowed to fight for the seat in November.

"Democrats will fight for FL-13 in the midterm, when the electorate is far less heavily tilted toward Republicans," said Democratic Congressional Campaign Committee Chairman Steve Israel (N.Y.).

Updated at 8:25 p.m.