1 21 22 package org.apache.derby.jdbc; 23 24 import org.apache.derby.iapi.reference.MessageId; 25 import org.apache.derby.iapi.services.i18n.MessageService; 26 27 import org.apache.derby.iapi.services.monitor.Monitor; 28 import org.apache.derby.iapi.jdbc.ResourceAdapter; 29 import org.apache.derby.iapi.db.Database; 30 31 import org.apache.derby.iapi.reference.Property; 32 33 import org.apache.derby.iapi.error.ExceptionSeverity; 34 35 import java.sql.SQLException ; 36 import java.sql.Connection ; 37 import java.sql.Driver ; 38 39 40 41 import javax.sql.XADataSource ; 42 import javax.sql.XAConnection ; 43 44 45 70 public class EmbeddedXADataSource extends EmbeddedDataSource implements 71 javax.sql.XADataSource 72 { 73 74 private static final long serialVersionUID = -5715798975598379738L; 75 76 transient private ResourceAdapter ra; 78 79 82 public EmbeddedXADataSource() 83 { 84 super(); 85 } 86 87 88 91 92 93 99 public final XAConnection getXAConnection() throws SQLException 100 { 101 if (ra == null || !ra.isActive()) 102 setupResourceAdapter(null, null, false); 103 104 return createXAConnection (ra, getUser(), getPassword(), false); 105 } 106 107 116 public final XAConnection getXAConnection(String user, String password) 117 throws SQLException 118 { 119 if (ra == null || !ra.isActive()) 120 setupResourceAdapter(user, password, true); 121 return createXAConnection (ra, user, password, true); 122 } 123 124 127 128 void update() { 129 ra = null; 130 super.update(); 131 } 132 133 private void setupResourceAdapter(String user, String password, boolean requestPassword) throws SQLException 134 { 135 synchronized(this) 136 { 137 if (ra == null || !ra.isActive()) 138 { 139 ra = null; 141 142 String dbName = getDatabaseName(); 143 if (dbName != null) { 144 145 Database database = null; 148 149 if (Monitor.getMonitor() != null) 152 database = (Database) 153 Monitor.findService(Property.DATABASE_MODULE, dbName); 154 155 if (database == null) 156 { 157 if (requestPassword) 161 getConnection(user, password).close(); 162 else 163 getConnection().close(); 164 165 database = (Database) 167 Monitor.findService(Property.DATABASE_MODULE, dbName); 168 } 169 170 if (database != null) 171 ra = (ResourceAdapter) database.getResourceAdapter(); 172 } 173 174 if (ra == null) 175 throw new SQLException (MessageService.getTextMessage(MessageId.CORE_DATABASE_NOT_AVAILABLE), 176 "08006", 177 ExceptionSeverity.DATABASE_SEVERITY); 178 179 180 findDriver(); 183 184 if (driver == null) 185 throw new SQLException (MessageService.getTextMessage(MessageId.CORE_DRIVER_NOT_AVAILABLE), 186 "08006", 187 ExceptionSeverity.DATABASE_SEVERITY); 188 189 } 190 } 191 } 192 193 199 protected XAConnection createXAConnection (ResourceAdapter ra, 200 String user, String password, 201 boolean requestPassword) throws SQLException { 202 return new EmbedXAConnection(this, ra, user, 203 password, requestPassword); 204 } 205 } 206 | Popular Tags |