I have three ms sql server:
A, primary
B, mirroring
C, witness
they are all in the same domain, so that A and B can failover automatically when there are some problem of these two DB.
I use glassfish as an app server and have set a connection pool with a connection string:
jdbc:sqlserver://ip_A:1433;failoverPartner=ip_B;databaseName=testDB;integratedSecurity=true
so that glassfish can ping to those DBs whenever which one is down, eg, if A is down, glassfish can connect to B, if B is down, glassfish connects to A.
I can use ping so that I am sure that the setting is correct.
I tried to write some test program of entity beans to test the failover function on the app perspective, but the result was not quite well. Every time I failover one DB then it took about 10 minutes to show the correct result again.
Here is my test program of entity bean, I just want to insert a record and print them out:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloJPAPU");//my persistent unit
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
Company c = new Company("AAA Co", (float)10.0, (float)2.0, (float)10.0, new Date().toString());
try
{
tx.begin();
em.persist(c); // persisting to the source
List<Company> list = em.createQuery("select c from Company c where c.companyName = :companyName").setParameter("companyName", c.getCompanyName()).getResultList();
for (int i=0; i<list.size(); i++)
{
out.println( list.get(i) + "<br>");
}
tx.commit();
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
em.close();
}
the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="
http://java.sun.com/xml/ns/persistence" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="HelloJPAPU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/__redirectDB</non-jta-data-source>
<class>server.Company</class>
<properties>
</properties>
</persistence-unit>
</persistence>
So my question is: Is there any way, setting on glassfish, or function of JPA that I can do the failover more efficiently or actually there is something wrong of my method??
Thanks!
[Message sent by forum member 'imyst918' (imyst918)]
http://forums.java.net/jive/thread.jspa?messageID=292624