I have a simple task to validate the sample code will switch over to the slave database when the master is unavailable. I simulate the master unavailable by stopping the master server; which generates the following exception below. What am I doing wrong? It appears the connection manager fails to try the second URL in the list: objectdb://10.9.2.15:9998//10.9.2.15:9999/test.odb;user=admin;password=password
Thank you in advance.
[ObjectDB 2.5.0_04] javax.persistence.PersistenceException Failed to connect to server 10.9.2.15:9999 (Connection refused: connect) (error 522) at com.objectdb.jpa.EMF.createEntityManager(EMF.java:253) at tutorial.Main.main(Main.java:22) Caused by: com.objectdb.o.UserException: Failed to connect to server 10.9.2.15:9999 (Connection refused: connect) at com.objectdb.o.MSG.d(MSG.java:74) at com.objectdb.o.CLS.M(CLS.java:153) at com.objectdb.o.CST.<init>(CST.java:65) at com.objectdb.o.CSF.UK(CSF.java:87) at com.objectdb.o.OMF.al(OMF.java:688) at com.objectdb.jpa.EMF.createEntityManager(EMF.java:250) ... 1 more Caused by: java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at com.objectdb.o.CLS.M(CLS.java:122) ... 5 more
Master Configuration
<objectdb> <general> <temp path="$temp" threshold="64mb" /> <network inactivity-timeout="5" /> <url-history size="50" user="true" password="true" /> <log path="$objectdb/log/" max="8mb" stdout="true" stderr="true" /> <log-archive path="$objectdb/log/archive/" retain="90" /> <logger name="*" level="info" /> </general> <database> <size initial="256kb" resize="256kb" page="2kb" /> <recovery enabled="true" sync="true" path="." max="128mb" /> <recording enabled="true" sync="true" path="." mode="write" /> <locking version-check="true" /> <processing cache="64mb" max-threads="10" /> <query-cache results="32mb" programs="500" /> <extensions drop="temp,tmp" /> <!-- <activation code="XXXX-XXXX-XXXX-XXXX-XXXX" /> --> </database> <entities> <enhancement agent="false" reflection="warning" /> <cache ref="weak" level2="0" /> <persist serialization="false" /> <cascade-persist always="auto" on-persist="false" on-commit="true" /> <dirty-tracking arrays="false" /> </entities> <schema> </schema> <server> <connection port="9999" max="100" /> <data path="$objectdb/db" /> </server> <users> <user username="admin" password="password"> <dir path="/" permissions="access,modify,create,delete" /> </user> <user username="$default" password="$$$###"> <dir path="/$user/" permissions="access,modify,create,delete"> <quota directories="5" files="20" disk-space="5mb" /> </dir> </user> <user username="user1" password="password" /> </users> <ssl enabled="false"> <server-keystore path="$objectdb/ssl/server-kstore" password="pwd" /> <client-truststore path="$objectdb/ssl/client-tstore" password="pwd" /> </ssl> </objectdb>
Slave Configuration
<objectdb> <general> <temp path="$temp" threshold="64mb" /> <network inactivity-timeout="5" /> <url-history size="50" user="true" password="true" /> <log path="$objectdb/log/" max="8mb" stdout="true" stderr="true" /> <log-archive path="$objectdb/log/archive/" retain="90" /> <logger name="*" level="info" /> </general> <database> <size initial="256kb" resize="256kb" page="2kb" /> <recovery enabled="true" sync="false" path="." max="128mb" /> <recording enabled="false" sync="false" path="." mode="all" /> <locking version-check="true" /> <processing cache="64mb" max-threads="10" /> <query-cache results="32mb" programs="500" /> <extensions drop="temp,tmp" /> <!-- <activation code="XXXX-XXXX-XXXX-XXXX-XXXX" /> --> </database> <entities> <enhancement agent="false" reflection="warning" /> <cache ref="weak" level2="0" /> <persist serialization="false" /> <cascade-persist always="auto" on-persist="false" on-commit="true" /> <dirty-tracking arrays="false" /> </entities> <schema> </schema> <server> <connection port="9998" max="100" /> <data path="$objectdb/db" /> <replication url="objectdb://localhost:9999/test.odb;user=admin;password=password" /> </server> <users> <user username="admin" password="password"> <dir path="/" permissions="access,modify,create,delete" /> </user> <user username="$default" password="$$$###"> <dir path="/$user/" permissions="access,modify,create,delete"> <quota directories="5" files="20" disk-space="5mb" /> </dir> </user> <user username="user1" password="password" /> </users> <ssl enabled="false"> <server-keystore path="$objectdb/ssl/server-kstore" password="pwd" /> <client-truststore path="$objectdb/ssl/client-tstore" password="pwd" /> </ssl> </objectdb>
Test Code
package tutorial; import javax.persistence.*; import java.util.*; public class Main { private static boolean WRITE_DATA_TO_DATABASE = false; public static void main(String[] args) { EntityManagerFactory emf = null; EntityManager em = null; try { emf = Persistence .createEntityManagerFactory("objectdb://10.9.2.15:9999/test.odb;user=admin;password=password|" + "objectdb://10.9.2.15:9998//10.9.2.15:9999/test.odb;user=admin;password=password"); for (int i = 0; i < 100; i++) { boolean flag = true; while(flag) { try { em = emf.createEntityManager(); flag = false; }catch(Exception e) { e.printStackTrace(); } } queryDatabase(em); System.out.println("COUNT = " + i); em.close(); try { Thread.sleep(1500); } catch (InterruptedException e) { // noop } } } catch (Exception e) { e.printStackTrace(); } finally { emf.close(); } } private static void queryDatabase(EntityManager em) { if (WRITE_DATA_TO_DATABASE) { em.getTransaction().begin(); for (int i = 0; i < 1000; i++) { Point p = new Point(i, i); em.persist(p); } em.getTransaction().commit(); } // Retrieve all the Point objects from the database: TypedQuery<Point> query = em.createQuery("SELECT p FROM Point p", Point.class); List<Point> results = query.getResultList(); int count = 0; for (Point p : results) { System.out.println(p); if(count++>5) { break; } } // Find the number of Point objects in the database: Query q1 = em.createQuery("SELECT COUNT(p) FROM Point p"); System.out.println("Total Points: " + q1.getSingleResult()); // Find the average X value: Query q2 = em.createQuery("SELECT AVG(p.x) FROM Point p"); System.out.println("Average X: " + q2.getSingleResult()); } }
package tutorial; import java.io.Serializable; import javax.persistence.*; @Entity public class Point implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private long id; private int x; private int y; public Point() { } Point(int x, int y) { this.x = x; this.y = y; } public Long getId() { return id; } public int getX() { return x; } public int getY() { return y; } @Override public String toString() { return String.format("(%d, %d)", this.x, this.y); } }