Advertisement
Guest User

Untitled

a guest
May 6th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. public class Test
  2. {
  3.     final static Logger logger = LoggerFactory.getLogger(Test.class);
  4.    
  5.     private static String host = "xenia.kubje.org";
  6.     private static int port = 3306;
  7.     private static String user = "jaka";
  8.     private static String password = "...";
  9.     private static String clusterName = "jaka_icluster";
  10.    
  11.     public static void main( String[] args ) throws Exception
  12.     {
  13.         Map<String, String> commonConfig = new HashMap<String, String>();
  14.         commonConfig.put(PersistenceUnitProperties.LOGGING_LEVEL, "FINER"); // FINEST
  15.        
  16.         logger.info("Creating cluster EMF...");
  17.         Map<String, String> clusterEmfConfig = new HashMap<String, String>(commonConfig);
  18.         clusterEmfConfig.put(PersistenceUnitProperties.JDBC_DRIVER, "com.mysql.jdbc.Driver");
  19.         clusterEmfConfig.put(PersistenceUnitProperties.JDBC_USER, user);
  20.         clusterEmfConfig.put(PersistenceUnitProperties.JDBC_PASSWORD, password);
  21.         clusterEmfConfig.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://"+host+":"+port+"/"+clusterName+"_global");
  22.         // Note how clusterEmf has the "local variable never read" warning, it's never used.
  23.         EntityManagerFactory clusterEmf = Persistence.createEntityManagerFactory("ClusterPU", clusterEmfConfig);
  24.                
  25.         logger.info("Creating instance EMF...");
  26.         Map<String, String> instanceEmfConfig = new HashMap<String, String>(commonConfig);
  27.         // Expected: Exception because no driver is set.
  28.         // Actual: works
  29.         EntityManagerFactory instanceEmf = Persistence.createEntityManagerFactory("InstancePU", instanceEmfConfig);
  30.        
  31.         logger.info("Creating an instance EM...");
  32.         int instanceId = 1;
  33.         Map<String, String> instanceEmConfig = new HashMap<String, String>();
  34.         instanceEmConfig.put(EntityManagerProperties.JDBC_DRIVER, "com.mysql.jdbc.Driver");
  35.         instanceEmConfig.put(EntityManagerProperties.JDBC_USER, user);
  36.         instanceEmConfig.put(EntityManagerProperties.JDBC_PASSWORD, password);
  37.         instanceEmConfig.put(EntityManagerProperties.JDBC_URL, "jdbc:mysql://"+host+":"+port+"/"+clusterName+"_instance_"+instanceId);
  38.         // Expected: a) Exception because no driver was set for EMF or b) use the driver for the EM
  39.         // Actual: Connections using data from clusterEmf are created! (database ..._global)
  40.         EntityManager instanceEm = instanceEmf.createEntityManager(instanceEmConfig);
  41.  
  42.         logger.info("Loading an entity from the instance EM...");
  43.  
  44.         Company c = instanceEm.find(Company.class, 66);
  45.         // Exception is thrown here: Table 'jaka_icluster_global.companies' doesn't exist
  46.         System.out.println(c.getName());
  47.     }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement