MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Bug54619.java
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package regression;
19 
20 import java.util.Properties;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.TimeUnit;
24 
25 import junit.framework.TestCase;
26 
27 import testsuite.clusterj.model.Employee;
28 
29 import com.mysql.clusterj.ClusterJHelper;
30 import com.mysql.clusterj.Session;
31 import com.mysql.clusterj.SessionFactory;
32 
33 public class Bug54619 extends TestCase {
34 
35  public void test() {
36  main(null);
37  }
38 
39  public static void main(String[] args) {
40 
41  /* http://bugs.mysql.com/bug.php?id=54619
42  *
43  * MySQL Cluster generic Linux and Windows 7.1.3
44  *
45  * Session factory in a different thread than sessions crashes JVM.
46  *
47  * If SessionFactory is initialized in a different thread than where
48  * sessions are created, session.newInstance call will crash JVM. This
49  * happens on Windows and Linux. In Windows the crash seems to be coming
50  * from DomainFieldHandlerImpl and from line table.getColumn(columnName);.
51  *
52  * Workaround is to insert a dummy row (most likely any db access works)
53  * from same thread where the SessionFactory is created. After this the
54  * session seem to be working from other thread.
55  */
56  ExecutorService pool = Executors.newFixedThreadPool(4);
57  // 1. Manager initializes session factory
58  Manager.createClusterJSession();
59  // 2. Remove the comment to get this to work.
60  //insert(1);
61 
62  // 3. Insert rows from different threads.
63  for (int i = 0; i < 4; ++i) {
64  final int id = i;
65  pool.submit(new Runnable() {
66 
67  public void run() {
68  Session session = Manager.getSessionFactory().getSession();
69  Employee entity = session.newInstance(Employee.class); // crash here?
70  entity.setId(id);
71  entity.setAge(id);
72  entity.setMagic(id);
73  entity.setName("Employee " + id);
74  session.currentTransaction().begin();
75  session.persist(entity);
76  session.currentTransaction().commit();
77  return;
78  }
79  });
80  }
81  // wait for all threads to complete
82  pool.shutdown();
83  try {
84  pool.awaitTermination(5, TimeUnit.SECONDS);
85  } catch (InterruptedException e) {
86  fail("Interrupted pool.awaitTermination");
87  }
88  pool.shutdownNow();
89  }
90 
91  private static void insert(int number) {
92  Session session = Manager.getSessionFactory().getSession();
93  Employee entity = session.newInstance(Employee.class);
94  entity.setId(number);
95  session.currentTransaction().begin();
96  session.persist(entity);
97  session.currentTransaction().commit();
98 
99  return;
100  }
101 
102 }
103 
104 class Manager {
105 
106  static SessionFactory sessionfactory;
107 
108  Manager() {
109  createClusterJSession();
110  return;
111  }
112 
113  static void createClusterJSession() {
114  Properties properties = new Properties();
115  properties.put("com.mysql.clusterj.connectstring", "localhost:9311");
116  properties.put("com.mysql.clusterj.database", "test");
117  sessionfactory = ClusterJHelper.getSessionFactory(properties);
118 
119  return;
120  }
121 
122  public static SessionFactory getSessionFactory() {
123  return sessionfactory;
124  }
125 }