parent
185188d6d1
commit
e1ec00fba5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue