在Spring MVC应用程序中,我具有@Transactional批注方法,在该方法中我可以调用(选择查询)内部的DAO层进行循环。
如果我的输入列表大小为“ 100”,那么我需要从数据库中选择100条记录。当服务层内部的方法用@Transactional注释时,我认为选择100条记录是一个工作单位,并且
我观察到对DAO层的每个语句/调用都会打开Connection。 我认为这会影响性能。
问题:
是否可以一次打开数据库连接并使用它直到结束 的所有查询(例如:如果输入列表大小为100,则为100个查询)?
如果按照当前的工作方式进行操作,是否会因为打开每个语句的数据库连接而影响性能?
服务实施:
@Servive
public class MyServiceImpl implements MyService{
@Autowired
private MyDao myDao;
@Transactional
@Override
public List<Object> getRecords(Request req) {
List<String> inputList = new ArrayList<String>();
for(String input inputList) {
try {
Object[] recordFromDB = mydao.fetchRecordsFromDB(input);
} catch(NoResultFoundException e) {
LOGGER.error("NoResultException caught while making a call to DB TABLE1");
}
}
}
}
Dao实施:
@Repository
public class MyDaoImpl implements MyDao{
@PersistentContext
private EntityManager em;
@Override
public Object[] fetchRecordsFromDB(input ip) {
Query q = em.createNativeQuery("select col1, col2, col3 from TABLE1 where col = ?1 and col2 = ?2 and col3 = ?3");
Object[] recordsDB = null;
recordsDB = q.getSingleResult();
return recordsDB;
}
}
日志:
如下所示,为每个语句完成了数据库连接。
09:35:56.815 TRACE o.h.i.SessionFactoryImpl$SessionBuilderImpl - Opening Hibernate Session. tenant=null, owner=org.hibernate.jpa.internal.EntityManagerImpl@7bd18d
09:35:56.947 TRACE o.h.s.i.AbstractServiceRegistryImpl - Initializing service [role=org.hibernate.stat.spi.StatisticsImplementor]
09:35:56.954 DEBUG o.h.s.internal.StatisticsInitiator - Statistics initialized [enabled=false]
09:35:56.955 TRACE org.hibernate.internal.SessionImpl - Opened session at timestamp: 15427893568
09:35:56.963 TRACE org.hibernate.internal.SessionImpl - Setting flush mode to: AUTO
09:35:56.964 TRACE org.hibernate.internal.SessionImpl - Setting cache mode to: NORMAL
09:35:57.243 TRACE o.h.engine.query.spi.QueryPlanCache - Unable to locate native-sql query plan in cache; generating (SELECT COL1, COL2, COL3 FROM TABLE1 WHERE COL1 = ?1 AND COL2 = ?2 AND ROWNUM = 1)
09:35:57.251 TRACE o.h.loader.custom.sql.SQLCustomQuery - Starting processing of sql query [SELECT COL1, COL2, COL3 FROM TABLE1 WHERE COL1 = ?1 AND COL2 = ?2 AND ROWNUM = 1]
09:35:57.267 TRACE org.hibernate.internal.SessionImpl - SQL query: SELECT COL1, COL2, COL3 FROM TABLE1 WHERE COL1 = ? AND COL2 = ? AND ROWNUM = 1
09:35:57.285 DEBUG org.hibernate.SQL - _SELECT_COL1,COL2,_COL3 _FROM_TABLE1 _WHERE_COL1 = ? _AND COL2 = ? _AND ROWNUM = 1
09:35:57.444 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Registering statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@18e6f11]
09:35:57.445 TRACE o.h.e.j.internal.JdbcCoordinatorImpl - Registering last query statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@18e6f11]
09:35:57.445 DEBUG org.hibernate.loader.Loader - bindNamedParameters() myValue2 -> 2 [2]
09:35:57.452 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [VARCHAR] - [ABC]
09:35:57.455 DEBUG org.hibernate.loader.Loader - bindNamedParameters() myValue1 -> 1 [1]
09:35:57.455 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [XYZ]
09:35:57.456 TRACE org.hibernate.loader.Loader - Bound [3] parameters total
09:35:57.521 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Registering result set [org.apache.commons.dbcp.DelegatingResultSet@17509f]
09:35:57.528 TRACE org.hibernate.loader.Loader - Processing result set
09:35:57.528 TRACE org.hibernate.loader.Loader - Done processing result set (0 rows)
09:35:57.530 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@18e6f11]
09:35:57.530 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Closing result set [org.apache.commons.dbcp.DelegatingResultSet@17509f]
09:35:57.530 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Closing prepared statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@18e6f11]
09:35:57.530 TRACE o.h.e.j.internal.JdbcCoordinatorImpl - Starting after statement execution processing [ON_CLOSE]
09:35:57.530 TRACE o.h.e.i.StatefulPersistenceContext - Initializing non-lazy collections
09:35:57.532 DEBUG o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
09:35:57.532 TRACE org.hibernate.internal.SessionImpl - Closing session
09:35:57.532 TRACE o.h.e.j.internal.JdbcCoordinatorImpl - Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@1e6c7f5]
09:35:57.532 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing JDBC resources
09:35:57.532 TRACE o.h.r.j.i.LogicalConnectionManagedImpl - Closing logical connection
09:35:57.535 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing JDBC resources
09:35:57.535 TRACE o.h.r.j.i.LogicalConnectionManagedImpl - Logical connection closed
09:35:57.537 ERROR c.c.a.s.i.MyServiceImpl - ----NoResultException caught while making a call to DB TABLE1 javax.persistence.NoResultException: No entity found for query
09:35:57.538 DEBUG o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler - Creating new EntityManager for shared EntityManager invocation
09:35:57.538 TRACE o.h.i.SessionFactoryImpl$SessionBuilderImpl - Opening Hibernate Session. tenant=null, owner=org.hibernate.jpa.internal.EntityManagerImpl@14d6023
09:35:57.538 TRACE org.hibernate.internal.SessionImpl - Opened session at timestamp: 15427893575
09:35:57.538 TRACE org.hibernate.internal.SessionImpl - Setting flush mode to: AUTO
09:35:57.538 TRACE org.hibernate.internal.SessionImpl - Setting cache mode to: NORMAL
09:35:57.538 TRACE o.h.engine.query.spi.QueryPlanCache - Located native-sql query plan in cache (SELECT COL1, COL2, COL3 FROM TABLE1 WHERE COL1 = ?1 AND COL2 = ?2 AND ROWNUM = 1)
09:35:57.538 TRACE org.hibernate.internal.SessionImpl - SQL query: SELECT COL1, COL2, COL3 FROM TABLE1 WHERE COL1 = ? AND COL2 = ? AND ROWNUM = 1
09:35:57.539 DEBUG org.hibernate.SQL - _SELECT_COL1,COL2,_COL3 _FROM_TABLE1 _WHERE_COL1 = ? _AND COL2 = ? _AND ROWNUM = 1
09:35:57.539 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Registering statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@1db1a33]
09:35:57.539 TRACE o.h.e.j.internal.JdbcCoordinatorImpl - Registering last query statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@1db1a33]
09:35:57.539 DEBUG org.hibernate.loader.Loader - bindNamedParameters() val2-> 2 [2]
09:35:57.539 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [VARCHAR] - [val2]
09:35:57.540 DEBUG org.hibernate.loader.Loader - bindNamedParameters() val2-> 1 [1]
09:35:57.542 TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [val1]
09:35:57.543 TRACE org.hibernate.loader.Loader - Bound [3] parameters total
09:35:57.549 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Registering result set [org.apache.commons.dbcp.DelegatingResultSet@1950c3f]
09:35:57.550 TRACE org.hibernate.loader.Loader - Processing result set
09:35:57.550 TRACE org.hibernate.loader.Loader - Done processing result set (0 rows)
09:35:57.550 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@1db1a33]
09:35:57.550 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Closing result set [org.apache.commons.dbcp.DelegatingResultSet@1950c3f]
09:35:57.550 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Closing prepared statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@1db1a33]
09:35:57.550 TRACE o.h.e.j.internal.JdbcCoordinatorImpl - Starting after statement execution processing [ON_CLOSE]
09:35:57.550 TRACE o.h.e.i.StatefulPersistenceContext - Initializing non-lazy collections
09:35:57.550 DEBUG o.s.o.jpa.EntityManagerFactoryUtils - Closing JPA EntityManager
09:35:57.550 TRACE org.hibernate.internal.SessionImpl - Closing session
09:35:57.550 TRACE o.h.e.j.internal.JdbcCoordinatorImpl - Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@570f5f]
09:35:57.550 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing JDBC resources
09:35:57.551 TRACE o.h.r.j.i.LogicalConnectionManagedImpl - Closing logical connection
09:35:57.551 TRACE o.h.r.j.i.ResourceRegistryStandardImpl - Releasing JDBC resources
09:35:57.552 TRACE o.h.r.j.i.LogicalConnectionManagedImpl - Logical connection closed
09:35:57.552 ERROR c.c.a.s.i.MyServiceImpl - ----NoResultException caught while making a call to DB TABLE1 furtherjavax.persistence.NoResultException: No entity found for query