1 22 package org.jboss.test.recover.derby; 23 24 import org.jboss.tm.recovery.Recoverable; 25 import org.jboss.tm.recovery.RecoveryManagerServiceMBean; 26 import org.jboss.system.ServiceMBeanSupport; 27 28 import javax.transaction.xa.XAResource ; 29 import javax.transaction.xa.Xid ; 30 import javax.transaction.xa.XAException ; 31 import javax.sql.XAConnection ; 32 import java.sql.DriverManager ; 33 import java.sql.SQLException ; 34 35 import org.apache.derby.jdbc.EmbeddedXADataSource; 36 37 43 public class EmbeddedDerbyRecoverable 44 extends ServiceMBeanSupport 45 implements Recoverable, EmbeddedDerbyRecoverableMBean 46 { 47 private String id; 48 private String databaseName; 49 private String user; 50 private String password; 51 private String createDatabase; 52 private RecoveryManagerServiceMBean manager; 53 private EmbeddedXADataSource xads; 54 private XAConnection connection; 55 private XAResource resource; 56 57 58 protected void startService() throws Exception 59 { 60 super.startService(); 61 xads = new EmbeddedXADataSource(); 62 xads.setDatabaseName(databaseName); 63 xads.setUser(user); 64 xads.setPassword(password); 65 xads.setCreateDatabase(createDatabase); 66 manager.registerRecoverable(this); 67 } 68 69 public void setManager(RecoveryManagerServiceMBean manager) 70 { 71 this.manager = manager; 72 } 73 74 75 public String getId() 76 { 77 return id; 78 } 79 80 public void setId(String id) 81 { 82 this.id = id; 83 } 84 85 public String getDatabaseName() 86 { 87 return databaseName; 88 } 89 90 public void setDatabaseName(String databaseName) 91 { 92 this.databaseName = databaseName; 93 } 94 95 public String getUser() 96 { 97 return user; 98 } 99 100 public void setUser(String user) 101 { 102 this.user = user; 103 } 104 105 public String getPassword() 106 { 107 return password; 108 } 109 110 public void setPassword(String password) 111 { 112 this.password = password; 113 } 114 115 public String getCreateDatabase() 116 { 117 return createDatabase; 118 } 119 120 public void setCreateDatabase(String createDatabase) 121 { 122 this.createDatabase = createDatabase; 123 } 124 125 public XAResource getResource() 126 { 127 try 128 { 129 connection = xads.getXAConnection(); 130 resource = connection.getXAResource(); 131 return resource; 132 } 133 catch (SQLException e) 134 { 135 throw new RuntimeException (e); 136 } 137 } 138 139 public Xid [] scan() throws XAException 140 { 141 return resource.recover(XAResource.TMSTARTRSCAN | XAResource.TMENDRSCAN); 142 } 143 144 public void cleanupResource() 145 { 146 try 147 { 148 System.out.println("EmbeddedDerbyRecoverable \"" + getServiceName() + "\": closing XA connection"); 149 connection.close(); 150 } 151 catch (SQLException e) 152 { 153 throw new RuntimeException (e); 154 } 155 } 156 } 157 | Popular Tags |