Thx for ur reply, how long u hav been using derby ??

On 2/19/08, musky <muskvar@gmail.com> wrote:

i am using derby server itself like a chat server.

i have two programs:they both have a main thread which creates a user
interface.
as the user(say bob) enters his  messages to the other party(say alice),they
are entered into a table say bobtable.
there is a thread that is invoked periodically that reads unread messages
from alicetable and displays them to bob and updates the messages as read by
setting the ID field to 1.

the same logic is for alice.

here are the programs:

bob:
import java.util.Timer;

import java.util.TimerTask;
import java.sql.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.*;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TestTime {
         DBClass dbobject1=new DBClass();
         static MainClass mc;
         static Statement stmt;
         static Connection conn=null;
         PreparedStatement pstmt=null;
         static PreparedStatement pstmt1=null;

         ResultSet rs=null;
         ResultSet rs1=null;

         static Display d;
           static Shell s;
           static Button OK;

         //int id=0;

public static void main(String[] args) throws Exception {
TestTime t = new TestTime();


DBClass dbobject1=new DBClass();
conn=dbobject1.createConnection("ChatDB1");
conn.setAutoCommit(false);
t.stmt = conn.createStatement();
t.execute();
//mc=new MainClass();

d = new Display( );
s = new Shell(d);
s.setSize(250,250);
s.setText("A Text Field Example");

final Text text1 = new Text(s, SWT.MULTI | SWT.BORDER);
text1.setBounds(0,0,250,150);
OK = new Button(s, SWT.PUSH);
OK.setBounds(80,170,75,40);
OK.setText("OK");
OK.addSelectionListener(new SelectionAdapter( ) {
          public void widgetSelected(SelectionEvent e)
          {
                  String contents = text1.getText( );
                  text1.setText("");



                System.out.println("Push Me Was Pushed");
                //System.out.println(contents);
                try
               {
               //stmt.executeUpdate("lock table bobtable in exclusive mode");
               pstmt1 = conn.prepareStatement("insert into bobtable
values(?,?)");
               pstmt1.setInt(1,0);
               pstmt1.setString(2,contents);

           pstmt1.executeUpdate();
              System.out.println("inserted "+contents+" into bobtable");
               }
                catch(Exception e1)
               {System.out.println("could not insert"+e1);}

          }

       });


s.open();

while(!s.isDisposed( )){
   if(!d.readAndDispatch( ))
       d.sleep( );
}

System.out.println("Main Method is finishing");

}

private void execute() throws Exception {
System.out.println(" Tasks scheduled");
int initialDelay = 1000; // start after 30 seconds
int period = 2000; // repeat every 5 seconds
Timer timer = new Timer();
System.out.println(" Timer set");

DBClass dbobject2=new DBClass();
final Connection conn2=dbobject2.createConnection("ChatDB1");
conn2.setAutoCommit(false);
stmt = conn2.createStatement();


//dbclass db=new dbclass();

timer.scheduleAtFixedRate(new TimerTask ()
{
public void run()
{
//System.out.println("Task Run");

//conn=dbobject1.createConnection("ChatDB1");


try
{
       //int id=mc.returnflag();

       //System.out.println(id);
         // System.out.println("Before ExecuteQuery");

   //stmt.executeUpdate("lock table alicetable in share mode");

PreparedStatement pstmt=conn2.prepareStatement("select * from alicetable
where TID=0");
//pstmt.setInt(1,id);

rs=pstmt.executeQuery();
//stmt.executeUpdate("lock table alicetable in exclusive mode");

stmt.executeUpdate("Update alicetable set TID=1 where TID=0");
//rs1=pstmt.executeQuery();


while(rs.next())
  {
          String str=rs.getString(2);
          int str2=rs.getInt(1);
          System.out.println(str2+"          "+str);
  }


}

catch ( Exception e ) {System.out.println("mhjfdshmnjh  "+e);}

}
}, initialDelay, period);
}
}

alice:
import java.util.Timer;

import java.util.TimerTask;
import java.sql.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.*;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TestTime2 {
         DBClass dbobject1=new DBClass();
         static MainClass mc;
         Statement stmt;
         static Connection conn=null;
         PreparedStatement pstmt=null;
         static PreparedStatement pstmt1=null;
     static Text text1;
         ResultSet rs=null;
         ResultSet rs1=null;

         static Display d;
           static Shell s;
           static Button OK;

         //int id=0;

public static void main(String[] args) throws Exception {
TestTime2 t = new TestTime2();


DBClass dbobject1=new DBClass();
conn=dbobject1.createConnection("ChatDB1");
conn.setAutoCommit(false);
t.execute();
//mc=new MainClass();

d = new Display( );
s = new Shell(d);
s.setSize(250,250);
s.setText("A Text Field Example");

text1 = new Text(s, SWT.MULTI | SWT.BORDER);
text1.setBounds(0,0,250,150);
OK = new Button(s, SWT.PUSH);
OK.setBounds(80,170,75,40);
OK.setText("OK");

OK.addSelectionListener(new SelectionAdapter( ) {
          public void widgetSelected(SelectionEvent e)
          {
                  String contents = text1.getText( );
                  text1.setText("");



                System.out.println("Push Me Was Pushed");
                //System.out.println(contents);
                try
               {
               pstmt1 = conn.prepareStatement("insert into alicetable
values(?,?)");
               pstmt1.setInt(1,0);
               pstmt1.setString(2,contents);

           pstmt1.executeUpdate();
              System.out.println("inserted "+contents+" into alicetable");
               }
                catch(Exception e1)
               {System.out.println("could not insert"+e1);}

          }

       });


s.open();

while(!s.isDisposed( )){
   if(!d.readAndDispatch( ))
       d.sleep( );
}

System.out.println("Main Method is finishing");

}

private void execute() throws Exception {
System.out.println(" Tasks scheduled");
int initialDelay = 10000; // start after 30 seconds
int period = 2000; // repeat every 5 seconds
Timer timer = new Timer();
System.out.println(" Timer set");

DBClass dbobject2=new DBClass();
final Connection conn2=dbobject2.createConnection("ChatDB1");
conn2.setAutoCommit(false);
stmt = conn2.createStatement();
//dbclass db=new dbclass();

timer.scheduleAtFixedRate(new TimerTask ()
{
public void run()
{
try
{
PreparedStatement pstmt=conn2.prepareStatement("select * from bobtable where
TID=0");
//pstmt.setInt(1,id);

rs=pstmt.executeQuery();
//stmt.executeUpdate("lock table bobtable in exclusive mode");

stmt.executeUpdate("Update bobtable set TID=1 where TID=0");
//rs1=pstmt.executeQuery();


while(rs.next())
  {
          String str=rs.getString(2);
          int str2=rs.getInt(1);
          System.out.println(str2+"          "+str);
          //text1.setText(str2+"                 "+str);
  }


}

catch ( Exception e ) {System.out.println("mhjfdshmnjh  "+e);}

}
}, initialDelay, period);
}
}

when i run both the programs:i get the following exception:mhjfdshmnjh
java.sql.SQLTransactionRollbackException: A lock could not be obtained
within the time requested

please help me.

regards.
--
View this message in context: http://www.nabble.com/Chat-program-using-derby-tp15561400p15561400.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.




--
! ! ! WACK ! ! !