Mikael Hakman created DERBY-6074:
------------------------------------

             Summary: DatabaseMetaData.getTables not updated in multi thread 
multi connection application
                 Key: DERBY-6074
                 URL: https://issues.apache.org/jira/browse/DERBY-6074
             Project: Derby
          Issue Type: Bug
          Components: JDBC
    Affects Versions: 10.9.1.0
         Environment: Windows 8
            Reporter: Mikael Hakman


The test application creates two connections (connection1 and connection2) on a 
separate threads. Then connection2 on thrid thread is used to create a table 
which is then committed on the main thread. connection1 is rollbacked and 
closed on main thread. New connection (connection3) is created on a separate 
thread. Then this connection on the main thread is used to retrieve 
DatabaseMetaData which is then used to get list of tables (getTables()). The 
newly created table is not listed but it should because it was commited. I can 
verify that the table is there by using ij. Also when the test is run the 
second time it complains that the table cannot be created because it is 
allready there, Here is the test program listing. I'm sorry I could'nt make it 
smaller. Thanks.
<pre>
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class TestDerby {
        
        public static void main (String [] args) {
                
                try {
                
                        new TestDerby ();
                        
                } catch (Exception ex) {
                        
                        ex.printStackTrace ();
                        
                        System.exit (1);
                }
        }

        Connection connection1;
        Connection connection2;
        
        public TestDerby () throws InterruptedException, SQLException {
                
                ConnectThread connectThread = new ConnectThread ();
                
                connectThread.start ();
                
                connectThread.join ();
                
                this.connection1 = connectThread.connection;
                
                connectThread = new ConnectThread ();
                
                connectThread.start ();
                
                connectThread.join ();
                
                this.connection2 = connectThread.connection;
                
                CreateTableThread createTableThread = new CreateTableThread ();
                
                createTableThread.start ();
                
                createTableThread.join ();
                
                this.connection2.commit ();
                
                this.connection1.rollback();
                
                this.connection1.close ();
                
                connectThread = new ConnectThread ();
                
                connectThread.start();
                
                connectThread.join ();
                
                this.connection1 = connectThread.connection;
                
                this.checkForTable ();
        }
        
        public void checkForTable () throws SQLException {
                
                DatabaseMetaData dmd = this.connection1.getMetaData();
                
                ResultSet tables = dmd.getTables(null,null,"%",null);
                
                while (tables.next()) {
                        
                        String tableName = tables.getString ("TABLE_NAME");
                        
                        if (tableName.equals("testtab")) {
                                
                                String tableSchema = tables.getString 
("TABLE_SCHEM");
                                
                                System.out.println ("Found table " + tableName 
+ " in " + tableSchema);
                        }
                }
                
                tables.close ();
        }
        
        public class ConnectThread extends Thread {
                
                Connection connection;
                
                public void run () {
                        
                        try {
                        
                                Class<?> driverClass = 
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                                
                                Driver driver = (Driver) 
driverClass.newInstance ();
                                
                                this.connection = 
driver.connect("jdbc:derby:testdb;create=true",new Properties());
                                
                                this.connection.setAutoCommit(false);
                                
                        } catch (Exception ex) {
                                
                                ex.printStackTrace ();
                                
                                System.exit (1);
                        }
                }
        }
        
        public class CreateTableThread extends Thread {
                
                public void run () {
                        
                        try {
                        
                                String sql = "create table testtab (            
   " +
                                         "      col1 integer not null primary 
key, " +
                                         "      col2 varchar(128)               
   " +
                                         ")                                    
" ;
                        
                                Statement stmt = connection2.createStatement ();
                                
                                stmt.execute (sql);
                                
                        } catch (SQLException ex) {
                                
                                ex.printStackTrace ();
                                
                                System.exit (1);
                        }
                }
        }
}
</pre>



--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to