Solve the lock table problem by working with both Erlang and Java in a single table

Source: Internet
Author: User

Zhangchangchang

1, problem description

The Erlang side writes an Oracle table through ODBC, and Java uses the JDBC driver to write the table, which is locked when the write operation occurs multiple times.

2. Problem solving

Idea: Using the adapter principle, suitable for Erlang and Java database connection, let Erlang end of the operation of the data table and Java side of the data table operation, in the order of sequencing, one end of the write operation is locked

The other end cannot operate until his operation completes the release lock and the other end can operate.

The adapter is written in Java and allows Erlang to communicate with Java via Otp.jar, and the connection of Erlang and Java to a data table operation is made from the adapter

Implementation method:

(1) Java side

public class Connectionadaptor {
Private static final Connectionadaptor instance = new Connectionadaptor ("Javanode", "Themailbox", "secret");
Private Otpnode node;
private static Otpmbox mbox;
Private Reentrantlock lock = new Reentrantlock ();
public static Connectionadaptor getinstance () {
return instance;
}
Private Connectionadaptor (String nodename,string mboxname,string cookie) {
Super ();
try{
Node=new Otpnode (Nodename,cookie);
}catch (IOException e) {
E.printstacktrace ();
}
System.out.print (node);
mbox = Node.creatembox (mboxname);
}

private void process () {
while (true) {
try{
Otperlangobject msg = mbox.receive ();
Otperlangtuple t = (otperlangtuple) msg;
Otperlangpid from = (otperlangpid) t.elementat (0);
String name = ((otperlangstring) T.elementat (1)). StringValue ();
if (Name.equals ("Write_start"))
Adaptor (1);
}catch (Exception e) {
}
}
}
public static void Main (string[] args) {

Initiates a thread for the listening of Erlang messages
New Thread () {
public void Run () {
Connectionadaptor.getinstance (). process ();
}
}.start ();
Start a thread for the Java side of the data table operation
New Thread () {
public void Run ()
{
for (int i=0;i<10;i++)
{
try{
try{
Connectionadaptor.getinstance (). Adaptor (2);
}catch (Exception e) {
E.printstacktrace ();
}
}catch (Exception e)
{
E.printstacktrace ();
}
}
}
}.start ();
}

public void adaptor (int type) {
Lock.lock ();
if (type = = 1)
{
while (true)
{
Otperlangobject msg = mbox.receive ();
Otperlangtuple t = (otperlangtuple) msg;
Otperlangpid from = ((otperlangstring) T.elementat (1)). StringValue ();
if (Name.equals ("Write_end"))
{
System.out.println ("Erlang Write Table End");
Lock.unlock ();
}
}
}
Else
{
Thread.Sleep (5000);
SYSTEM.OUT.PRINTLN ("Java Write table End");
Lock.unlock ();
}
}
}

Erlang side:

-module (Erl_to_java).
-export ([WRITE_TABLE/0,START/1]).
Write_table ()
{Themailbox,[email protected]}! {self (), "Write_start"},
Timer:sleep (5000),
{Themailbox,[email protected]}! {self (), "Write_end"}.

Start (N)
Case N =:= 0 of
True-OK;
False--write_table (), Start (N-1)
End.

3. Timing Diagram

4. Summary of issues

(1) Java Singleton mode

(2) Java and Erlang communication

(3) thread lock mechanism between Java threads

First, a single-instance mode is used to obtain an adapter object, and then a thread is started to execute process () to listen for a write-table message from the Erlang side, and once Erlang makes a write-table request, it needs to be locked and executed in the Erlang process.

After the table operation, the Java process is sent a write-off request, and then the Java process releases the lock, once the Java side Write table operation, then get the lock to write table operation, then release the lock, in the Java writing table, Erlang's write table operation must wait until the lock is released, and vice versa.

5. How to use

(1) Running the Java side of the adapter needs to install the ERL operating environment, need to import Otperlang.jar package;

(2) When the Erlang end node is started, it should be consistent with the cookie when the adapter singleton is created, and

Erl-sname Erlangnode-setcookie secret-pa "Erl_to_java.bin's Path"-eval "net_adm:ping ([email protected])"

Solve the lock table problem by working with both Erlang and Java in a single table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.