001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.sql.xa;
019
020import java.sql.Connection;
021import java.sql.SQLException;
022
023import javax.sql.XAConnection;
024import javax.sql.XADataSource;
025import javax.xml.bind.annotation.XmlType;
026
027import net.sf.hajdbc.management.Description;
028import net.sf.hajdbc.management.MBean;
029import net.sf.hajdbc.sql.CommonDataSourceDatabase;
030
031/**
032 * A database described by an {@link XADataSource}.
033 * @author Paul Ferraro
034 */
035@MBean
036@Description("Database accessed via a server-side XADataSource")
037@XmlType(name = "database")
038public class XADataSourceDatabase extends CommonDataSourceDatabase<XADataSource>
039{
040        private boolean force2PC = false;
041        
042        public XADataSourceDatabase()
043        {
044                super(XADataSource.class);
045        }
046        
047        /**
048         * {@inheritDoc}
049         * @see net.sf.hajdbc.Database#connect(java.lang.Object, java.lang.String)
050         */
051        @Override
052        public Connection connect(XADataSource dataSource, String password) throws SQLException
053        {
054                XAConnection connection = this.requiresAuthentication() ? dataSource.getXAConnection(this.getUser(), password) : dataSource.getXAConnection();
055                
056                return connection.getConnection();
057        }
058
059        public boolean isForce2PC()
060        {
061                return this.force2PC;
062        }
063        
064        public void setForce2PC(boolean force)
065        {
066                this.force2PC = force;
067        }
068}