We are trying to get the connection object from the EntityManager
Below is the sample code
final Session unwrap = proxy.unwrap(Session.class);
unwrap.doWork(new Work()
{
#Override
public void execute(Connection connection) throws SQLException
{
PreparedStatement ps = connection.prepareStatement(MY_QUERY);
for (Object value : valueSet)
{
....
....
ps.addBatch();
}
try
{
int[] ints = ps.executeBatch();
} finally
{
ps.close();
}
}
});
This works fine .
The concern we have is that when this code is invoked , everytime getConnection is called on the DataSource. Does that mean a new connection is obtained from the pool ?
This has performance impact in our use case .
Our understanding is that the current active connection will be utilised.
Is the understanding incorrect ?
The Hibernate documentation says:
Controller for allowing users to perform JDBC related work using the Connection managed by this Session.
So it is the (single) Connection used by the Session.
Everything else would be a Bug.
Related
I code my Test project and it is prohibited to use Spring and Hibernate there.
I wanted to manage my transactions from Service layer.
For this I have created a class that gets a Connection from the pool and puts it in the ThreadLocal.
This is an example of the fields and the method.
private static ThreadLocal<Connection> threadLocalConnection;
private ComboPooledDataSource comboPooledDataSource ;
public boolean createConnectionIfAbsent() {
boolean isConnectionCreated = false;
try {
Connection currentConnection = threadLocalConnection.get();
if(currentConnection == null) {
Connection conn = this.comboPooledDataSource.getConnection();
conn.setAutoCommit(false);
threadLocalConnection.set(conn);
isConnectionCreated = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return isConnectionCreated;
}
The class has also close, rollback methods.
Here is the example of how I manage Connections in a Service Layer.
public BigDecimal getTotalOrdersCount() {
boolean connectionCreated = DBManager.getInstance().createConnectionIfAbsent();
BigDecimal ordersCount = BigDecimal.ZERO;
try {
ordersCount = orderDao.getRowNumber();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (connectionCreated) DBManager.getInstance().closeConnection();
}
return ordersCount;
}
Dao just uses this to get the connection.
Connection connection = DBManager.getInstance().getConnection();
I found no other way to manage connections in a Servlet project from a Service layer, could you please tell if it is ok? If not - what drawbacks does it have and what should I use instead.
UPD:
Please pay attention to this Service method. Let's assume that Each method in DAO gets the Connection from a pool and closes it.
I do know that I need connection.setAutoCommit(false); to start a transaction, but what to do it in this kind of a situation?
When a single methods calls 2 DAO.
Just give up on a transaction handling?
void setStatusDeclinedAndRefund() {
// sets Order status to DECLINED
// refund money to user's balance
}
No.
Don't second guess the connection pool. Use it in the standard way: get a connection, use it, close it.
There is no need to use the same connection for every database interaction in a given thread. Also, you'll have serious liveliness problems if you allocate each thread a connection, because typically there are way more request processing threads than there are connections in the pool.
I'm currently working on a college project, and I'm creating a very simple e-commerce style website.
I'm using JDBC driver manager and connection pool for the connection to the db, while using Tomcat 9.0 as the container.
The problem is: when I modify some product through the website (let's say the amount available for example), the website doesn't always reflect the changes, while I can always see the data correctly in MySql Workbench.
It actually works one time out of two on the same query:
I run the query for the first time after the changes -> it shows the old value
I run the query for the second time after the changes -> it shows the new value
I run the query for the third time after the changes -> it shows the old value
And so on.
I've already tried to set caching off (from the query, using the SQL_NO_CACHE), but it didn't seem to solve the problem, I've tried to use Datasource instead, but it causes other problems that most likely I won't have the time to solve.
This is the connection pool file, which I think might be problem, I'm not that sure tho:
public class DriverManagerConnectionPool {
private static List<Connection> freeDbConnections;
static {
freeDbConnections = new LinkedList<Connection>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("DB driver not found:"+ e.getMessage());
}
}
private static synchronized Connection createDBConnection() throws SQLException {
Connection newConnection = null;
String ip = "localhost";
String port = "3306";
String db = "storage";
String username = "root";
String password = "1234";
newConnection = DriverManager.getConnection("jdbc:mysql://"+ ip+":"+ port+"/"+db+"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", username, password);
newConnection.setAutoCommit(false);
return newConnection;
}
public static synchronized Connection getConnection() throws SQLException {
Connection connection;
if (!freeDbConnections.isEmpty()) {
connection = (Connection) freeDbConnections.get(0);
freeDbConnections.remove(0);
try {
if (connection.isClosed())
connection = getConnection();
} catch (SQLException e) {
connection.close();
connection = getConnection();
}
} else {
connection = createDBConnection();
}
return connection;
}
public static synchronized void releaseConnection(Connection connection) throws SQLException {
if(connection != null) freeDbConnections.add(connection);
}
}
I really hope you can help me, I haven't found any solution online!
I guess it is because of auto-commit is disabled. Please try using #Transactional or set auto-commit to true. You can also try to use db.commit after each statement.
As per your connection pool implementation, all connection in your pool seems to be auto committed false.
Please check you have properly committed the connection after executing the query or not.
So it might be the case that, when executing the query after changes with same connection it reflects those changes, done earlier and on other connections, old values are might get returned.
I have a spring component like following:
#Component
class FooComponent {
#Autowired
private DataSource dataSource;
public void execute() {
try (SQLServerConnection connection = dataSource.getConnection().unwrap(SQLServerConnection.class)) {
// some logic here with connection
}
}
}
I have to unwrap the connection because I'm using some API from Microsoft's JDBC driver (it throws exeption if I'm just passing dataSource.getConnection()).
So my question is:
I'm correctly getting the connection in execute method? Can that piece of code cause "connection leaks"?
I'm asking this because at some point I saw in logs a error like: Could not get JDBC Connection; nested exception is org.apache.tomcat.jdbc.pool.PoolExhaustedException. (I'm not asking how to solve this error or what it means)
So my question is: I'm correctly getting the connection in execute method?
No, the pool returns connection proxies rather than real ones (See ProxyConnection).
And the proxy's close method is "overridden" to return the connection to the pool:
if (compare(CLOSE_VAL,method)) {
if (connection==null) return null; //noop for already closed.
PooledConnection poolc = this.connection;
this.connection = null;
pool.returnConnection(poolc);
return null;
}
}
But when you do
try (SQLServerConnection connection = dataSource.getConnection().unwrap(SQLServerConnection.class))
the close method called on real connection, not on the proxy. The connection is not returned to the pool and eventually the pool throws PoolExhaustedException.
Here is how it can be fixed:
try (Connection connection = dataSource.getConnection()) { // proxy is returned to the pool
SQLServerConnection c = connection.unwrap(SQLServerConnection.class));
// Work with SQLServerConnection
}
Also remember that you should leave the unwrapped connection in the same state as you acquired it
I have a JDBC batch update operation which might take long time, hence I am using transaction timeout to handle this.
#Override
#Transactional(propagation=Propagation.REQUIRES_NEW,timeout=10)
public void saveAllUsingBatch(List<KillPrintModel> list){
PreparedStatmentMapper ps= new HibernateDao.PreparedStatmentMapper<KillPrintModel>() {
#Override
public void prepareStatement(PreparedStatement ps, KillPrintModel t)
throws SQLException {
ps.setString(1, t.getOffice());
ps.setString(2, t.getAccount());
ps.setDate(3, new java.sql.Date(t.getUpdatedOn().getTime()));
}
};
String sql = String.format("INSERT INTO dbo.%s (%s,%s,%s) VALUES (?,?,?)",KillPrintModel.TABLE_NAME,KillPrintModel.FIELD_Office,KillPrintModel.FIELD_Account,KillPrintModel.FIELD_UpdatedOn);
this.jdbcBatchOperation(list, sql, ps);
}
This method goes on for more than a minute(and returns successfully) even when I have a transaction time out of 10 seconds. It works fine when the timeout is 0.
Is it because My thread is always in running state once it starts execution ?
If debugging in trace mode does not help, just put a breakpoint in the following hibernate classes, they ultimately set a timeout in the preparedstatement.setQueryTimeout(...) from the #Transactional Annotation
org.hibernate.engine.jdbc.internal.StatementPreparerImpl
private void setStatementTimeout(PreparedStatement preparedStatement) throws SQLException {
final int remainingTransactionTimeOutPeriod = jdbcCoordinator.determineRemainingTransactionTimeOutPeriod();
if ( remainingTransactionTimeOutPeriod > 0 ) {
preparedStatement.setQueryTimeout( remainingTransactionTimeOutPeriod );
}
}
Or even better, as early as the transaction manager and step through until you hit the statement.setQueryTimout(..).
org.springframework.orm.hibernate4.HibernateTransactionManager
int timeout = determineTimeout(definition);
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
// Use Hibernate's own transaction timeout mechanism on Hibernate 3.1+
// Applies to all statements, also to inserts, updates and deletes!
hibTx = session.getTransaction();
hibTx.setTimeout(timeout);
hibTx.begin();
}
else {
// Open a plain Hibernate transaction without specified timeout.
hibTx = session.beginTransaction();
}
I am just getting started with jsp and my question is this - when I have a singleton class, how do I tidy up after it?
In particular:
public class DBConnection {
private static Connection connection = null;
private static Statement statement = null;
public static ResultSet executeQuery(String query){
if (connection == null) { /*initConnection*/ }
if (statement == null) { /*initStatement*/ }
// do some stuff
}
}
Now, I use this class in several pages to get results from jdbc. However, I need to eventually call statement.close(); and connection.close(); - when should I call those?
I am using singleton, because it felt wrong to call for connection to a database over and over whenever I needed to make a query.
The Connection must be closed always, and after you have executed all your database statements for the desired operations. Two examples:
Case 1: You must show a list of products to user filtered by criteria from database. Solution: get a connection, retrieve a list of products using the filter criteria, close the connection.
Case 2: The client selects some of these products and updates the minimum stock to get an alert and restock them. Solution: get a connection, update all the products, close the connection.
Based on these cases, we can learn lot of things:
You can execute more than a single statement while having/maintaining a single connection open.
The connection should live only in the block where it is used. It should not live before or after that.
Both cases can happen at the same time since they are in a multi threaded environment. So, a single database connection must not be available to be used by two threads at the same time, in order to avoid result problems. For example, user A searches the products that are in category Foo and user B searches the products that are in category Bar, you don't want to show the products in category Bar to user A.
From last sentence, each database operation ((or group of similar operations like Case 2) should be handled in an atomic operation. To assure this, the connection must not be stored in a singleton object, instead it must be live only in the method being used.
In consequence:
Do not declare the Connection nor the Statement nor the ResultSet nor other JDBC resource as static. It will simply fail. Instead, declare only the Connection as field of your DBConnection class. Let each method decide to handle each Statement (or PreparedStatement) and ResultSet and specific JDBC resources.
Since you must close the connection after its usage, then add two more methods: void open() and void close(). These methods will handle the database connection retrieval and closing that connection.
Additional, since the DBConnection looks like a wrapper class for Connection class and database connection operations, I would recommend to have at least three more methods: void setAutoCommit(boolean autoCommit), void commit() and void rollback(). These methods will be plain wrappers for Connection#setAutoCommit Connection#close and Connection#rollback respectively.
Then you can use the class in this way:
public List<Product> getProducts(String categoryName) {
String sql = "SELECT id, name FROM Product WHERE categoryName = ?";
List<Product> productList = new ArrayList<Product>();
DBConnection dbConnection = new DBConnection();
try {
dbConnection.open();
ResultSet resultSet = dbConnection.executeSelect(sql, categoryName); //execute select and apply parameters
//fill productList...
} catch (Exception e) {
//always handle your exceptions
...
} finally {
//don't forget to also close other resources here like ResultSet...
//always close the connection
dbConnection.close();
}
}
Note that in this example the PreparedStatement is not in the getProducts method, it will be a local variable of the executeSelect method.
Additional notes:
When working in an application server, you should not open connections naively e.g. using Class.forName("..."), instead use a database connection pool. You can roll on some database connection pooling libraries like C3P0 as explained here: How to establish a connection pool in JDBC?. Or configure one in your application server, as I explain here: Is it a good idea to put jdbc connection code in servlet class?
If this is for learning purposes, then roll on your own classes to handle the communication with your database. In real world applications, this is not recommended (doesn't mean you should not do it). Instead, use a database connectivity framework like ORMs e.g. JPA (Java official ORM framework) or Hibernate; there are no ORM frameworks that handles database communication like Spring JDBC and MyBatis. The choice is yours.
More info:
Should a database connection stay open all the time or only be opened when needed?
How do servlets work? Instantiation, sessions, shared variables and multithreading. Not directly related to your question, but it will help you understand why to not maintain state in resources that are used in multithreaded environments.
Define connection resource in mywebapp/META-INF/context.xml file
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="2" maxWait="20000"
driverClassName="com.mysql.jdbc.Driver"
username="myuser" password="mypwd"
url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"
validationQuery="SELECT 1" />
Create DB.java helper class to minimize code in other parts of app
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
public class DB {
public static Connection createConnection() throws SQLException {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mydb");
return ds.getConnection();
} catch (SQLException ex) {
throw ex;
} catch (Exception ex) {
SQLException sqex = new SQLException(ex.getMessage());
sqex.initCause(ex);
throw sqex;
}
}
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) try { rs.close(); } catch (Exception e) { }
if (stmt != null) try { stmt.close(); } catch (Exception e) { }
if (conn != null) try { conn.close(); } catch (Exception e) { }
}
public static void close(ResultSet rs, boolean closeStmtAndConn) {
if (rs==null) return;
try {
Statement stmt = rs.getStatement();
close(rs, stmt, stmt!=null ? stmt.getConnection() : null);
} catch (Exception ex) { }
}
}
And somewhere in your app DAO code use DB helper.
public List<MyBean> getBeans() throws SQLException {
List<MyBean> list = new ArrayList<MyBean>();
ResultSet rs=null;
try {
Connection con = DB.createConnection();
String sql = "Select * from beantable where typeid=?";
PreparedStatement stmt = con.prepareStatement(sql, Statement.NO_GENERATED_KEYS);
stmt.setInt(1, 101);
rs = stmt.executeQuery();
while(rs.next()
list.add( createBean(rs) );
} finally {
DB.close(rs, true); // or DB.close(rs, stmt, conn);
}
return list;
}
private MyBean createBean(ResultSet rs) throws SQLException {
MyBean bean = new MyBean();
bean.setId( rs.getLong("id") );
bean.setName( rs.getString("name" );
bean.setTypeId( rs.getInt("typeid") );
return bean;
}
I would add two methods to the class:
public static void open() throws SomeException;
public static void close() throws SomeException;
then your calling code looks something like this{
try {
DBConnection.open();
... code to use the connection one or more times ...
} finally {
DBConnection.close();
}
Wrap all your database calls inside that and it will take care of closing whether there is an exception thrown or not.
Of course, this isn't much different than having a regular class, which I might recommend:
try {
DBConnection conn = new DBConnection();
conn.open();
... all the code to use the database (but you pass 'conn' around) ...
} finally {
conn.close();
}
And you might want to look at the java.lang.AutoCloseable and java.io.Closeable to see if that helps you.
2
If you are keeping it open across page loads, there isn't any place to put the try ... finally stuff so you can open it and close it when the servlet closes or the server closes or something like that.
If you are going to leave it open, you need to make sure and add code to verify it doesn't close when you aren't looking. A short network glitch, for example, could close it down. In that case, you need to reopen it when it gets closed. Otherwise, all database access from that point will fail.
You might want to look into the concept of a DataBase Pool. Apache has one -- DBCP. Tomcat has its own that's quite good. Other containers, like JBOSS, WebSphere, WebLogic all have them. There's a couple that can be used with the Spring Framework. What it does is manage one or more database connections. Your code asks it for one and it returns an open one, unless none is available and then it opens one and returns it. You call close when your code gets through with it but it doesn't really close the connection, it just returns it to the pool.
You can usually configure the pool to check for shut down connections and reopen if needed.