diff --git a/BusinessProject/.DS_Store b/BusinessProject/.DS_Store index 954795a..206cf68 100644 Binary files a/BusinessProject/.DS_Store and b/BusinessProject/.DS_Store differ diff --git a/BusinessProject/.classpath b/BusinessProject/.classpath index c87c538..5f033ec 100644 --- a/BusinessProject/.classpath +++ b/BusinessProject/.classpath @@ -46,5 +46,7 @@ + + diff --git a/BusinessProject/WebContent/.DS_Store b/BusinessProject/WebContent/.DS_Store index 574e8a8..a135453 100644 Binary files a/BusinessProject/WebContent/.DS_Store and b/BusinessProject/WebContent/.DS_Store differ diff --git a/BusinessProject/WebContent/WEB-INF/.DS_Store b/BusinessProject/WebContent/WEB-INF/.DS_Store index a9275ec..2c5e6b0 100644 Binary files a/BusinessProject/WebContent/WEB-INF/.DS_Store and b/BusinessProject/WebContent/WEB-INF/.DS_Store differ diff --git a/BusinessProject/bin/.DS_Store b/BusinessProject/bin/.DS_Store index 5670402..3313aa9 100644 Binary files a/BusinessProject/bin/.DS_Store and b/BusinessProject/bin/.DS_Store differ diff --git a/BusinessProject/bin/DataBase/.DS_Store b/BusinessProject/bin/DataBase/.DS_Store index ab2b86f..5278ba4 100644 Binary files a/BusinessProject/bin/DataBase/.DS_Store and b/BusinessProject/bin/DataBase/.DS_Store differ diff --git a/BusinessProject/bin/DataBase/JTAConnManager.class b/BusinessProject/bin/DataBase/JTAConnManager.class index bf38c1d..5ba09d4 100644 Binary files a/BusinessProject/bin/DataBase/JTAConnManager.class and b/BusinessProject/bin/DataBase/JTAConnManager.class differ diff --git a/BusinessProject/bin/Operation/.DS_Store b/BusinessProject/bin/Operation/.DS_Store new file mode 100644 index 0000000..db78fd1 Binary files /dev/null and b/BusinessProject/bin/Operation/.DS_Store differ diff --git a/BusinessProject/bin/Transaction/ContextManager.class b/BusinessProject/bin/Transaction/ContextManager.class new file mode 100644 index 0000000..ec126e5 Binary files /dev/null and b/BusinessProject/bin/Transaction/ContextManager.class differ diff --git a/BusinessProject/bin/Transaction/DataBaseManager.class b/BusinessProject/bin/Transaction/DataBaseManager.class new file mode 100644 index 0000000..fba0f60 Binary files /dev/null and b/BusinessProject/bin/Transaction/DataBaseManager.class differ diff --git a/BusinessProject/bin/Transaction/JTATransaction.class b/BusinessProject/bin/Transaction/JTATransaction.class new file mode 100644 index 0000000..29c0df6 Binary files /dev/null and b/BusinessProject/bin/Transaction/JTATransaction.class differ diff --git a/BusinessProject/bin/Transaction/MessageSender.class b/BusinessProject/bin/Transaction/MessageSender.class new file mode 100644 index 0000000..1848ae7 Binary files /dev/null and b/BusinessProject/bin/Transaction/MessageSender.class differ diff --git a/BusinessProject/bin/Transaction/TransTest.class b/BusinessProject/bin/Transaction/TransTest.class new file mode 100644 index 0000000..91d8eca Binary files /dev/null and b/BusinessProject/bin/Transaction/TransTest.class differ diff --git a/BusinessProject/bin/View/.DS_Store b/BusinessProject/bin/View/.DS_Store index 5338ca5..d1ecded 100644 Binary files a/BusinessProject/bin/View/.DS_Store and b/BusinessProject/bin/View/.DS_Store differ diff --git a/BusinessProject/lib/.DS_Store b/BusinessProject/lib/.DS_Store index 968382e..4d09c0a 100644 Binary files a/BusinessProject/lib/.DS_Store and b/BusinessProject/lib/.DS_Store differ diff --git a/BusinessProject/lib/jotm/.DS_Store b/BusinessProject/lib/jotm/.DS_Store new file mode 100644 index 0000000..26d93b0 Binary files /dev/null and b/BusinessProject/lib/jotm/.DS_Store differ diff --git a/BusinessProject/lib/jotm/commons-cli-1.0.jar b/BusinessProject/lib/jotm/commons-cli-1.0.jar new file mode 100755 index 0000000..22a004e Binary files /dev/null and b/BusinessProject/lib/jotm/commons-cli-1.0.jar differ diff --git a/BusinessProject/lib/jotm/commons-logging.jar b/BusinessProject/lib/jotm/commons-logging.jar new file mode 100755 index 0000000..b99c937 Binary files /dev/null and b/BusinessProject/lib/jotm/commons-logging.jar differ diff --git a/BusinessProject/lib/jotm/connector-1_5.jar b/BusinessProject/lib/jotm/connector-1_5.jar new file mode 100755 index 0000000..6341789 Binary files /dev/null and b/BusinessProject/lib/jotm/connector-1_5.jar differ diff --git a/BusinessProject/lib/jotm/howl-0.1.11.jar b/BusinessProject/lib/jotm/howl-0.1.11.jar new file mode 100755 index 0000000..78f74f1 Binary files /dev/null and b/BusinessProject/lib/jotm/howl-0.1.11.jar differ diff --git a/BusinessProject/lib/jotm/jotm.jar b/BusinessProject/lib/jotm/jotm.jar new file mode 100755 index 0000000..d9cf3f7 Binary files /dev/null and b/BusinessProject/lib/jotm/jotm.jar differ diff --git a/BusinessProject/lib/jotm/jotm_iiop_stubs.jar b/BusinessProject/lib/jotm/jotm_iiop_stubs.jar new file mode 100755 index 0000000..1946913 Binary files /dev/null and b/BusinessProject/lib/jotm/jotm_iiop_stubs.jar differ diff --git a/BusinessProject/lib/jotm/jotm_jrmp_stubs.jar b/BusinessProject/lib/jotm/jotm_jrmp_stubs.jar new file mode 100755 index 0000000..cf348f0 Binary files /dev/null and b/BusinessProject/lib/jotm/jotm_jrmp_stubs.jar differ diff --git a/BusinessProject/lib/jotm/jta-spec1_0_1.jar b/BusinessProject/lib/jotm/jta-spec1_0_1.jar new file mode 100755 index 0000000..705e8c8 Binary files /dev/null and b/BusinessProject/lib/jotm/jta-spec1_0_1.jar differ diff --git a/BusinessProject/lib/jotm/jts1_0.jar b/BusinessProject/lib/jotm/jts1_0.jar new file mode 100755 index 0000000..2e57881 Binary files /dev/null and b/BusinessProject/lib/jotm/jts1_0.jar differ diff --git a/BusinessProject/lib/jotm/objectweb-datasource.jar b/BusinessProject/lib/jotm/objectweb-datasource.jar new file mode 100755 index 0000000..3bbc07f Binary files /dev/null and b/BusinessProject/lib/jotm/objectweb-datasource.jar differ diff --git a/BusinessProject/lib/jotm/ow_carol.jar b/BusinessProject/lib/jotm/ow_carol.jar new file mode 100755 index 0000000..c7801ec Binary files /dev/null and b/BusinessProject/lib/jotm/ow_carol.jar differ diff --git a/BusinessProject/lib/jotm/xapool-1.5.0.jar b/BusinessProject/lib/jotm/xapool-1.5.0.jar new file mode 100755 index 0000000..747cee9 Binary files /dev/null and b/BusinessProject/lib/jotm/xapool-1.5.0.jar differ diff --git a/BusinessProject/src/DataBase/JTAConnManager.java b/BusinessProject/src/DataBase/JTAConnManager.java index f044d1e..00a6cb9 100644 --- a/BusinessProject/src/DataBase/JTAConnManager.java +++ b/BusinessProject/src/DataBase/JTAConnManager.java @@ -47,8 +47,8 @@ public class JTAConnManager try { - dataSource1 = (DataSource) ctx.lookup("java:comp/env/jndi/mysql1"); - dataSource2 = (DataSource) ctx.lookup("java:comp/env/jndi/mysql2"); + dataSource1 = (DataSource) ctx.lookup("mysql1"); + dataSource2 = (DataSource) ctx.lookup("mysql2"); } catch (NamingException e) diff --git a/BusinessProject/src/Transaction/ContextManager.java b/BusinessProject/src/Transaction/ContextManager.java new file mode 100644 index 0000000..74f32b0 --- /dev/null +++ b/BusinessProject/src/Transaction/ContextManager.java @@ -0,0 +1,47 @@ +package Transaction; + +import java.util.Properties; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +public class ContextManager +{ + final static private String url = "t3://localhost:7001"; + final static private String user = "xmoe"; + final static private String password = "cbiloli7"; + + + static private Context ctx = null; + + static public Context getContext() + { + if(ctx != null) + return ctx; + + Properties properties = null; + properties = new Properties(); + properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); + properties.put(Context.PROVIDER_URL, url); + + if (user != null) + { + properties.put(Context.SECURITY_PRINCIPAL, user); + properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); + } + + try + { + ctx = new InitialContext(properties); + } + catch (NamingException e) + { + // TODO Auto-generated catch block + System.err.println("Failed to create context!"); + e.printStackTrace(); + } + + return ctx; + } +} diff --git a/BusinessProject/src/Transaction/DataBaseManager.java b/BusinessProject/src/Transaction/DataBaseManager.java new file mode 100644 index 0000000..098aace --- /dev/null +++ b/BusinessProject/src/Transaction/DataBaseManager.java @@ -0,0 +1,127 @@ +package Transaction; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + + +public class DataBaseManager +{ + private static DataBaseManager instance; + private DataSource dataSource1; + private DataSource dataSource2; + +// private DataSource dataSource; + + static + { + instance = new DataBaseManager(); + } + + public DataBaseManager() + { + + Context ctx = ContextManager.getContext(); + + try + { + dataSource1 = (DataSource) ctx.lookup("mysql1"); + dataSource2 = (DataSource) ctx.lookup("mysql2"); + + } + catch (NamingException e) + { + // TODO Auto-generated catch block + System.err.println("Failed to find context"); + e.printStackTrace(); + } + } + + public final static DataBaseManager getInstance() + { + return instance; + } + + public final Connection getConnection1() + { + try + { + return dataSource1.getConnection(); + } + catch (SQLException e) + { + throw new RuntimeException("无法从数据源获取连接 database1", e); + } + } + + public final Connection getConnection2() + { + try + { + return dataSource2.getConnection(); + } + catch (SQLException e) + { + throw new RuntimeException("无法从数据源获取连接 database2", e); + } + } + + + public void TestConn1() throws SQLException + { + Connection con = null; + try + { + con = DataBaseManager.getInstance().getConnection1(); + ResultSet rs = con.createStatement().executeQuery("select * from t_user"); + + while(rs.next()) + { + System.out.println(rs.getString(1)); + } + rs.getStatement().close(); + } + catch (Exception e) + { + System.err.println("Connecting(1)...error"); + e.printStackTrace(); + } + finally + { + if (con != null) + con.close(); + } + } + + public void TestConn2() throws SQLException + { + Connection con = null; + try + { + con = DataBaseManager.getInstance().getConnection2(); + ResultSet rs = con.createStatement().executeQuery("select * from t_user"); + + while(rs.next()) + { + System.out.println(rs.getString(1)); + } + rs.getStatement().close(); + } + catch (Exception e) + { + System.err.println("Connecting(2)...error"); + e.printStackTrace(); + } + finally + { + if (con != null) + con.close(); + } + } +} diff --git a/BusinessProject/src/Transaction/JTATransaction.java b/BusinessProject/src/Transaction/JTATransaction.java new file mode 100644 index 0000000..22e5f56 --- /dev/null +++ b/BusinessProject/src/Transaction/JTATransaction.java @@ -0,0 +1,32 @@ +package Transaction; + +import java.util.Properties; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import javax.transaction.UserTransaction; + +public class JTATransaction +{ + static private UserTransaction utx = null; + + static public UserTransaction getUserTransaction() + { + if(utx != null) + return utx; + + try + { + utx = (UserTransaction) ContextManager.getContext() + .lookup("javax/transaction/UserTransaction"); + } + catch (NamingException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return utx; + } +} diff --git a/BusinessProject/src/Transaction/MessageSender.java b/BusinessProject/src/Transaction/MessageSender.java new file mode 100644 index 0000000..540af64 --- /dev/null +++ b/BusinessProject/src/Transaction/MessageSender.java @@ -0,0 +1,75 @@ +package Transaction; + +import java.util.Properties; + +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.Queue; +import javax.jms.QueueSession; +import javax.jms.Session; +import javax.jms.Topic; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.naming.Context; + +public class MessageSender +{ + static private MessageSender instance = null; + + static public MessageSender getMessageSender() + { + if(instance == null) + instance = new MessageSender(); + + return instance; + } + + public void sendMessage() + { + try + { + Context ctx = ContextManager.getContext(); + + ConnectionFactory cf = null; + cf = (ConnectionFactory)ctx.lookup("weblogic/jms/ConnectionFactory"); + Connection cn = cf.createConnection(); + cn.start(); + + Session session = cn.createSession(false, QueueSession.AUTO_ACKNOWLEDGE); + + Topic q = (Topic)ctx.lookup("TopicTest"); + MessageProducer p = session.createProducer(q); + + for(int i=1; i<=1; i++) + { + Message msg1 = session.createTextMessage("NON_PERSISTENT"+i); + msg1.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT); + p.send(msg1); + + Message msg2 = session.createTextMessage("PERSISTENT"+i); + msg2.setJMSDeliveryMode(DeliveryMode.PERSISTENT); + p.send(msg2); + } + + p.close(); + cn.close(); + + } + catch (NamingException e) + { + System.err.println("Context Error!"); + e.printStackTrace(); + } + catch (JMSException e) + { + // TODO Auto-generated catch block + System.err.println("JMS Error!"); + e.printStackTrace(); + } + } +} diff --git a/BusinessProject/src/Transaction/TransTest.java b/BusinessProject/src/Transaction/TransTest.java new file mode 100644 index 0000000..e411c28 --- /dev/null +++ b/BusinessProject/src/Transaction/TransTest.java @@ -0,0 +1,235 @@ +package Transaction; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Properties; + +import javax.jms.ConnectionFactory; +import javax.jms.DeliveryMode; +import javax.jms.Message; +import javax.jms.MessageProducer; +import javax.jms.QueueSession; +import javax.jms.Session; +import javax.jms.Topic; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.sql.DataSource; +import javax.transaction.UserTransaction; + +public class TransTest +{ + static UserTransaction utx = null; + static DataSource dataSource1 = null; + static DataSource dataSource2 = null; + static ConnectionFactory cf = null; + static Topic topic = null; + + static + { + try + { + Context context = ContextManager.getContext(); + utx = (UserTransaction) JTATransaction.getUserTransaction(); + dataSource1 = (DataSource) DataBaseManager.getInstance().getConnection1(); + dataSource2 = (DataSource) DataBaseManager.getInstance().getConnection2(); + + cf = (ConnectionFactory)context.lookup("weblogic/jms/ConnectionFactory"); + topic = (Topic)context.lookup("TopicTest"); + + } + catch(Exception e) + { + System.err.println("Error : In static block"); + e.printStackTrace(); + } + } + + + public boolean testMultiDatasource() + { + Connection cn1 = null; + Connection cn2 = null; + Statement stm1 = null; + Statement stm2 = null; + + try + { + + cn1 = dataSource1.getConnection(); + stm1 = cn1.createStatement(); + ResultSet rs1 = stm1.executeQuery("select * from t_user"); + + while(rs1.next()) + { + System.out.println(rs1.getString(1)); + } + + cn2 = dataSource2.getConnection(); + stm2 = cn2.createStatement(); + ResultSet rs2 = stm2.executeQuery("select * from t_user"); + + while(rs2.next()) + { + System.out.println(rs2.getString(1)); + } + return true; + } + catch (Exception e) + { + e.printStackTrace(); + return false; + } + finally + { + try + { + if (stm1 != null) + stm1.close(); + + if (cn1 != null) + cn1.close(); + + if (stm2 != null) + stm2.close(); + + if (cn2 != null) + cn2.close(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + } + + + public boolean testJTA() + { + Connection cn1 = null; + Connection cn2 = null; + Statement stm1 = null; + Statement stm2 = null; + + try + { + cn1 = dataSource1.getConnection(); + cn2 = dataSource2.getConnection(); + + utx.begin(); + MessageSender.getMessageSender().sendMessage(); + stm1 = cn1.createStatement(); + stm1.execute("insert into t_user values('20160019','utx_test')"); +// stm2 = cn2.createStatement(); +// stm2.execute("insert into t_user values('20160001','utx_test')"); + utx.commit(); + return true; + } + catch (Exception e) + { + e.printStackTrace(); + try + { + utx.rollback(); + } + catch (Exception e1) + { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + return false; + } + finally + { + try + { + if (stm1 != null) + stm1.close(); + + if (cn1 != null) + cn1.close(); + + if (stm2 != null) + stm2.close(); + + if (cn2 != null) + cn2.close(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + } + + public boolean testMultiResourceJTA() + { + + Connection cn1 = null; + Statement stm1 = null; + javax.jms.Connection cn = null; + try + { + cn1 = dataSource1.getConnection(); + + utx.begin(); + + cn = cf.createConnection(); + cn.start(); + Session session = cn.createSession(false, QueueSession.AUTO_ACKNOWLEDGE); + MessageProducer p = session.createProducer(topic); + + for(int i=1;i<=1;i++) + { + Message msg1 = session.createTextMessage("NON_PERSISTENT"+i); + msg1.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT); + p.send(msg1); + } + p.close(); + session.close(); + + stm1 = cn1.createStatement(); + stm1.execute("insert into t_user values('20160020','utx_test')"); + + utx.commit(); + return true; + } + catch (Exception e) + { + e.printStackTrace(); + try + { + utx.rollback(); + } + catch (Exception e1) + { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + return false; + } + finally + { + try + { + if (stm1 != null) + stm1.close(); + + if (cn1 != null) + cn1.close(); + + if (cn != null) + cn.close(); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + } + + public boolean testJMS() + { + return true; + } +}