In the code below, I was expecting the in-memory database to be deleted when the connection closes as described at https://www.sqlite.org/inmemorydb.html
public static void main(String[] args) throws SQLException {
var connection = DriverManager.getConnection("jdbc:sqlite::memory");
var statement = connection.createStatement();
statement.execute("CREATE TABLE IF NOT EXISTS tab (name);");
statement.execute("INSERT INTO tab (name) VALUES ('foo');");
var result = statement.executeQuery("SELECT * from tab;");
while (result.next()) {
System.out.println(result.getString("name"));
}
connection.close();
}
In fact what I see is that each time I run this code there is an extra row in the table implying that the database is persisting between calls. Why is that happening?
Turned out it was a simple error. The connection string should have been:
var connection = DriverManager.getConnection("jdbc:sqlite::memory:");
The missing final : meant that it was interpreting it as as a database called "memory".
Related
We have a Utility Class that opens and disconnects the Derby DB Connection
The database is stored in a folder on C drive of a Windows 7 computer
The application is being written with JavaFX 8
We are NOT using transactions
Before using the Utility Class we would Open and Disconnect the connection with each CRUD function rs.close() con.close()
Our question has two parts
1. Do we really need to open and close the connection with each CRUD function?
2. Why is the Utility Class not closing the rs and stmnt?
The conn.close() fires when
We will post the code for the Utility Class and the Delete function
We are also using the code below in the Main Class to shutdown the Derby DB when the application is closed
private void handle(WindowEvent e) throws SQLException {
//Proper CLOSE of connection to DB keeps proper incrementing by 1 as set when the table is created
JDBCUtil.closeConnection(conn);
String conURL = "jdbc:derby:;shutdown=true";
try{
DriverManager.getConnection(conURL);
}catch (SQLException se){
if(!(se.getErrorCode() == 50000) && (se.getSQLState().equals("XJ015")))
System.err.println(se);
}
System.exit(0);
Platform.exit();
}
Utility Class
public class JDBCUtil {
public static Connection conn;
public static Connection getConnection() throws SQLException {
// The URL is specific to the JDBC driver and the database you want to connect
String dbName="InfoDB";
String dbURL = "jdbc:derby:C:/A_DerbyDataBase/DBName/" + dbName + ";create=true";
//String dbURL = "jdbc:derby:DATABASE_NAME;create=true";
// Set the user id and password
//String userId = "app";
//String password = "app";
// Get a connection
conn = DriverManager.getConnection(dbURL);
// Set the auto-commit to false ONLY if you use Transactions
/*conn.setAutoCommit(true);*/
System.out.println("111111111111111111111111111 Get Connection ");
return conn;
}
public static void closeConnection(Connection conn) throws SQLException {
if (conn != null) {
System.out.println("222222222222222222222 conn.close ");
conn.close();
}
}
public static void closeStatement(Statement stmnt) throws SQLException{
if (stmnt != null) {
System.out.println("3333333333333333333333 stmnt.close ");
stmnt.close();
}
}
public static void closeResultSet(ResultSet rs) throws SQLException {
if (rs != null) {
System.out.println("44444444444444444444444 rs.close ");
rs.close();
}
}
/*public static void commit(Connection conn) throws SQLException {
if (conn != null) {
conn.commit();
}
}
public static void rollback(Connection conn) throws SQLException {
if (conn != null) {
conn.rollback();
}
}*/
public static void main(String[] args) throws SQLException {
//conn = JDBCUtil.getConnection();
JDBCUtil.closeConnection(conn);
}
And the Delete Function
#FXML
private void onDelete(ActionEvent e) throws SQLException, IOException{
conn = JDBCUtil.getConnection();
String sql = "DELETE FROM infodata WHERE ID = ?";
pstmt = conn.prepareStatement(sql);
int ID = Integer.valueOf(txfID.getText());
pstmt.setInt(1, ID);
pstmt.executeUpdate();
pstmt.close();
JDBCUtil.closeConnection(conn);
ReadFromDB();
btnEdit.setVisible(false);
btnDelete.setVisible(false);
btnCancel.setVisible(false);
btnAdd.setVisible(true);
txfInfo.setText("Record Deleted");
}
Database Access with a Connection Pool
Rather than writing your own connection logic, I advise using a connection pool, for example see the Baeldung Hikari Tutorial. You can use it like this:
Initialize the connection pool in your application's init method.
When you want to use a connection, use the Java try-with-resources construct, which will auto-close the connection (returning it to the connection pool) when you are done with it.
In your application's stop method, close the connection pool.
Sample wrapper class for DataSource connections (copied from Baeldung Hikari link above):
public class DataSource {
private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
static {
config.setJdbcUrl( "jdbc_url" );
config.setUsername( "database_username" );
config.setPassword( "database_password" );
config.addDataSourceProperty( "cachePrepStmts" , "true" );
config.addDataSourceProperty( "prepStmtCacheSize" , "250" );
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , "2048" );
ds = new HikariDataSource( config );
}
private DataSource() {}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
Sample database call using the Hikari connection pool with autoclose of connections using Java's try with resources construct (copied from Baeldung Hikari link above):
public static List<Employee> fetchData() throws SQLException {
String SQL_QUERY = "select * from emp";
List<Employee> employees = null;
try (Connection con = DataSource.getConnection();
PreparedStatement pst = con.prepareStatement( SQL_QUERY );
ResultSet rs = pst.executeQuery();) {
employees = new ArrayList<>();
Employee employee;
while ( rs.next() ) {
employee = new Employee();
employee.setEmpNo( rs.getInt( "empno" ) );
employee.setEname( rs.getString( "ename" ) );
employee.setJob( rs.getString( "job" ) );
employee.setMgr( rs.getInt( "mgr" ) );
employee.setHiredate( rs.getDate( "hiredate" ) );
employee.setSal( rs.getInt( "sal" ) );
employee.setComm( rs.getInt( "comm" ) );
employee.setDeptno( rs.getInt( "deptno" ) );
employees.add( employee );
}
}
return employees;
}
For more info read the Baeldung tutorial and the Hikari site documentation:
Link to HikariCP project.
Database Access without a Connection Pool
Now, you don't need to use a connection pool to do this, you can open and close a connection for each database call, however I would recommend using a connection pool for performance reasons and so that you don't end up trying to re-invent the wheel and ending up with something square rather than round.
I didn't try debugging or examining the connection utility class you have in your question, but, if you are going to replace it with a connection pool anyway, there is no reason to do that.
Sample code for accessing a database from JavaFX without a connection pool:
JavaFX MySQL connection example please
Non-Task based database access.
JavaFX - Background Thread for SQL Query which uses a Task based sample for db access.
Some of the sample code is written to be minimal to demonstrate particular purposes, such as access to a database and feedback of data from a given table to a UI, and not as a general purpose database utility. For a robust implementation for an application that uses many database queries, a connection pool or a dedicated database connection manager class (such as you have in your question), is preferred.
When I execute the following code
public async Task<ObservableCollection<CommentModel>> GetTypeWiseComment(int refId, int commentType)
{
try
{
var conn = _dbOperations.GetSyncConnection(DbConnectionType.UserDbConnetion);
var sqlCommand = new SQLiteCommand(conn)
{
CommandText = "bit complex sqlite query"
};
List<CommentModel> commentList = null;
Task commentListTask =
Task.Factory.StartNew(() => commentList = sqlCommand.ExecuteQuery<CommentModel>().ToList());
await commentListTask;
var commentsList = new ObservableCollection<CommentModel>(commentList);
return commentsList;
}
catch (Exception)
{
throw;
}
finally
{
GC.Collect();
}
}
Sometimes I get the following exception
Message: database is locked
InnerException: N/A
StackTrace: at SQLite.SQLite3.Prepare2(IntPtr db, String query)
at SQLite.SQLiteCommand.Prepare()
at SQLite.SQLiteCommand.<ExecuteDeferredQuery>d__12<com.IronOne.BoardPACWinAppBO.Meeting.MeetingModel>.MoveNext()
at System.Collections.Generic.List<System.Diagnostics.Tracing.FieldMetadata>..ctor(Collections.Generic.IEnumerable<System.Diagnostics.Tracing.FieldMetadata> collection)
at BoardPACWinApp!<BaseAddress>+0xaa36ca
at com.IronOne.BoardPACWinAppDAO.Comments.CommentsDAO.<>c__DisplayClass4_0.<GetCommentTypeWiseComment>b__0()
at SharedLibrary!<BaseAddress>+0x38ec7b
at SharedLibrary!<BaseAddress>+0x4978cc
Can anyone point out what's wrong with my code?
There is another sync process going on the background and sometimes it has a bulk of records which may take more than 10 seconds to execute. If this above code happens to execute at the same time as the sync writes to the DB, it might block the reads, right?
If so how do I read from SQLite while another process writes to the DB?
Thank you.
as #Mark Benningfield mentioned enabling WAL mode almost solved my problem. However, there was another issue that creates a lot of SQLite connections on my app so I solved that by creating a Singleton module which handles database connections.
Please comment and ask if you require more information if you encounter a similar issue. Thanks.
Taken from: https://learn.microsoft.com/en-us/azure/cosmos-db/create-graph-dotnet
Am getting a exception on the .wait() part:
NullReferenceException: Object reference not set to an instance of an object.
at Gremlin.Net.Driver.Connection.ReceiveAsync[T]()
at Gremlin.Net.Driver.Connection.SubmitAsync[T](RequestMessage requestMessage)
at Gremlin.Net.Driver.ProxyConnection.SubmitAsync[T](RequestMessage requestMessage)
at Gremlin.Net.Driver.GremlinClient.SubmitAsync[T](RequestMessage requestMessage)
at Gremlin.Net.Driver.GremlinClientExtensions.SubmitAsync[T](IGremlinClient gremlinClient, String requestScript, Dictionary`2 bindings)
Code:
private static string database = "db";
private static string collection = "col";
private static string hostname = "grem-test.gremlin.cosmosdb.azure.com";
public void test()
{
var gremlinServer = new GremlinServer(hostname, 443, enableSsl: true,
username: "/dbs/" + database + "/colls/" + collection,
password: authKey);
var gremlinClient = new GremlinClient(gremlinServer);
var grem = "g.V()";
var t = gremlinClient.SubmitAsync<dynamic>(grem);
t.Wait();
foreach (var result in t.Result)
{
// The vertex results are formed as dictionaries with a nested dictionary for their properties
string output = JsonConvert.SerializeObject(result);
Console.WriteLine(String.Format("\tResult:\n\t{0}", output));
}
It should be:
var task = gremlinClient.SubmitAsync<dynamic>(grem);
task.Wait();
Taken from Gremlin C# Samples:
// Create async task to execute the Gremlin query.
var task = gremlinClient.SubmitAsync<dynamic>(query.Value);
task.Wait();
I started with the sample application where it uses:
private static Task<ResultSet<dynamic>> SubmitRequest(GremlinClient gremlinClient, string query)
{
try
{
return gremlinClient.SubmitAsync<dynamic>(query);
}
catch (ResponseException e)
{
// They have extra stuff here for the request information that isn't relevant
throw;
}
}
I expanded from there and never had any issues besides the occasional exception from trying to run a query while another one was still working. I can only assume that running the queries this way works better than directly calling SubmitAsync().
One other thing I would recommend is double-checking your values for the server parameters just in case.
I have a class that gets tables from Sql Server. the class is static, but the variables are not. I want to know if it is OK in Asp net, because I had read not to use static at database in Asp net.
My Class: (There are more functions in the class, I put here one for example)
public static class DataBase
{
public static bool TableChange(string sqlCreate)
{
using (SqlConnection connection = new SqlConnection(Global.ConnectionString))
{
using (var cmd = new SqlCommand(sqlCreate, connection))
{
try
{
connection.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Log.WriteLog(ex.Message + "\n" + sqlCreate, ex, HttpContext.Current.Request);
return false;
}
}
}
return true;
}
}
Thanks in advance
What you have read is most probably something to do with this approach:
public static EntityContext Database = new EntityContext();
// or
public static SqlConnection Database = new SqlConnection("...");
Here you store the database connection in a static variable and thus all parallel requests would want to use the same connection which is a very bad approach if it even works at all (it will probably work sort of fine until the page is under load).
You do not have this problem, because in your case only the methods are static, not the variables. Your code follows the recommended path - open connection (retrieve it from the pool), execute query, close the connection (return it to the pool).
I have got an ASP.Net website, where the data is brought in from ISeries.
The data connection to ISeries is quite slow and the speed is quite important for this website. Because of the slow speed of data retrieval from ISeries, I want to make as less database connections as possible.
So, I was thinking about storing tables from the database which rarely changes as static properties in my website. Whenevera user logs in I submit a thread which refreshes the data in the static property.
Is this approach correct? If not, what are the problems with this approach and what are the possible alternatives?
Example:-
For list of ports, I submit the below thread when user logs on:-
// Get Ports list
Thread threadPorts = new Thread(delegate()
{
Ports.getPortList();
});
threadPorts.Start();
Session["threadPorts"] = threadPorts;
In class Ports, there are 2 methods -
one for populating the static property PortList,
and the other checks if the thread is alive and waits for the thread to complete and retrieve the list of ports, once it is complete. The second method is the one which I use in my application whenever I need the list of ports (populating a dropdown, etc).
public static void getPortList()
{
DataTable dt = new DataTable();
DB2Connection conn = new DB2Connection(ConfigurationManager.ConnectionStrings["db2IBM"].ConnectionString);
conn.Open();
string query = query to get ports from ISeries;
DB2Command cmd = new DB2Command(query, conn);
cmd.CommandType = CommandType.Text;
DB2DataAdapter adp = new DB2DataAdapter(cmd);
adp.Fill(dt);
cmd.Dispose();
conn.Close();
List<Port> list = new List<Port>();
foreach (DataRow row in dt.Rows)
{
list.Add(new Port(row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString()));
}
StaticProp.PortList = list;
}
public static List<Port> getPortListfromSession()
{
List<Port> portList = new List<Port>();
if (System.Web.HttpContext.Current.Session["threadPorts"] != null)
{
Thread t = (Thread)System.Web.HttpContext.Current.Session["threadPorts"];
if (t != null)
{
if (t.IsAlive)
{
t.Join();
}
}
}
if (System.Web.HttpContext.Current.Session["threadPorts"] != null)
System.Web.HttpContext.Current.Session.Remove("threadPorts");
portList = StaticProp.PortList;
return portList;
}
I take it that ISeries, is an external database!
Why not take data from that database and stick it in your own, and update it separately?
You can then query your own database quickly, and update your database, as often as you see fit, alternatively you can use a file, I personally my preferred file data format is Json, over XML - but database is much better.