Ajax + SSH + EJB + Oracle interview Summary (medium)

Source: Internet
Author: User

1. Hibernate interview question: how to get the size of a Collection instead of initializing it?
Integer size = (Integer) s. createFilter (collection, "select count (*)"). uniqueResult
2. How to Implement JDBC and Hibernate paging?
1) Hibernate paging:
Query query = session. createQuery ("from Student ");
Query. setFirstResult (firstResult); // you can specify the number of records starting from each page.
Query. setMaxResults (resultNumber); // you can specify the number of records displayed on each page.
Collection students = query. list ();
2) JDBC paging: Different SQL paging statements are used for different databases.
For example, the SQL statement in Oracle is: "SELECT * FROM (SELECT a. *, rownum r FROM
TB_STUDENT) WHERE r between 2 and 10 "query records from record 2 to record 10
All records
3. How to optimize HIBERNATE performance?
In general, the main considerations for HIBERNATE performance tuning are as follows:
Ø database design adjustment
Ø HQL Optimization
Ø correct use of APIS (for example, selecting different sets and querying APIs based on different service types)
Ø main configuration parameters (log, query cache, fetch_size, batch_size, etc)
Optimization of ing files (ID generation policy, level-2 cache, delayed loading, and association optimization)
Ø Level 1 Cache Management
There are also many unique policies for level-2 caching.
Transaction control policy.
1. Database Design
A) reduce the complexity of Association
B) Try not to use the federated primary key
C) ID generation mechanism. Different databases provide different mechanisms.
D) Appropriate redundant data, but the pursuit of high paradigm
2. HQL Optimization
Aside from its association with some caching mechanisms of HIBERNATE, HQL's optimization skills are the same as common SQL optimization skills, so it is easy to find some experience on the Internet.
3. Master Configuration
A) query cache, which is different from the cache described below. It is a cache for HQL statements, that is, cache data can be used for re-execution of identical statements. However, the query cache in a transaction system (data changes frequently and the probability of the same query conditions is not large) may be counterproductive: it will consume a lot of system resources in vain, but it is difficult to come in handy.
B) fetch_size, which is similar to JDBC-related parameters. The larger the parameter is, the better. You should set it according to business characteristics.
C) Same as batch_size.
D) In the production system, remember to turn off SQL statement printing.
4. Cache
A) database-level cache: this level of cache is the most efficient and secure, but different database management levels are not the same. For example, in ORACLE, you can specify to cache the entire table during table creation.
B) SESSION cache: the cache is valid in a HIBERNATE SESSION. This level of cache is not reliable and is more efficient than HIBERNATE's automatic management. However, it provides a method to clear the cache, this is effective in bulk Add/update operations. For example, if you add 100,000 records at the same time, you may find an OutofMemeroy exception. In this case, you may need to manually clear the cache: Session. evict and Session. clear.
C) Application cache: it is effective in a SESSIONFACTORY, so it is also the top priority of optimization. Therefore, there are many policies to consider. Before putting data into this cache level, consider the following prerequisites:
I. data will not be modified by a third party (for example, is there another application that is also modifying the data ?)
Ii. The data is not too big
Iii. data will not be updated frequently (otherwise, using the CACHE may be counterproductive)
Iv. Data is frequently queried.
V. Data is not key data (such as money and security issues ).
The cache can be configured in several formats in the ing file: read-only (read-only, applicable to static/historical data with few changes), nonstrict-read-write, read-write (common format with average efficiency), transactional (in JTA, few caching products are supported)
D) distributed cache: the configuration is the same as that of c), but the selection of cache products is different. Currently, there are not many options available in HIBERNATE, such as oscache and jboss cache. Most of the current projects, they are conservative in using clusters (especially key transaction systems. In a cluster environment, database-level cache is the safest.
5. Delayed Loading
A) object delayed loading: implemented through dynamic proxy
B) SET delayed loading: HIBERNATE provides support for this by implementing its own SET/LIST.
C) Delayed attribute loading:
6. Method Selection
A) to do the same thing, HIBERNATE provides some options, but the specific method may affect performance/code. It is shown that 100,000 records (List/Set/Bag/Map) are returned for processing at a time, which may result in insufficient memory. If you use a cursor (ScrollableResults) or Iterator result set.
B) the load/get method of the Session. The former uses the second-level cache, while the latter does not.
C) Query and list/iterator. If you study them carefully, you may find many interesting situations. The main differences between the two are: (if Spring is used, find in HibernateTemplate, iterator method ):
I. list can only use the query cache (but it does not play a major role in the query cache in the transaction system). It cannot use a single entity in the second-level cache, but the objects identified by list will be written to the second-level cache, however, it generally generates only a small number of SQL statements for execution. In many cases, it is one (unassociated) statement ).
Ii. iterator can use the second-level cache. For a query statement, it first finds the IDs of all qualified records from the database, and then uses the IDs to cache the query, for records not in the cache, you can easily find them from the database by constructing statements. If no matching record exists in the cache, using iterator will generate N + 1 SQL statement (N is the number of qualified records)
Iii. Use iterator and Cache Management APIs to solve memory problems in massive data queries, such:
While (it. hasNext ()){
YouObject object = (YouObject) it. next ();
Session. evict (youObject );
SessionFactory. evice (YouObject. class, youObject. getId ());
}
If the list method is used, the OutofMemory error may occur.
Iv. As described above, I think you should know how to use these two methods.
7. Set Selection
Detailed descriptions are provided in "3.1. Understanding Collection performance" in HIBERNATE 19.5.
8. Transaction Control
The main impact of transactions on performance includes the selection of transaction methods, transaction isolation levels, and lock selection.
A) transaction method selection: if you do not involve multiple Transaction Manager transactions, you do not need to use JTA. Only JDBC transaction control is supported.
B) transaction isolation level: see standard SQL transaction isolation level.
C) Selection of locks: Pessimistic locks (usually implemented by the specific transaction Manager), low efficiency but secure for long transactions. Optimistic lock (usually implemented at the application level). For example, you can define the VERSION field in HIBERNATE. Obviously, if multiple applications operate on data and these applications do not use the same optimistic lock mechanism, the optimistic lock will become invalid. Therefore, there should be different policies for different data. As in many cases above, we often find a balance between efficiency and security/accuracy, optimization is not a pure technical problem. You should have a sufficient understanding of your applications and business features.
9. batch operation
Even when JDBC is used to update a large amount of data, the efficiency of BATCH differs greatly from that of no BATCH. You can set batch_size to support batch operations.
For example, to delete objects in a table in batches, such as the "delete Account" Statement, HIBERNATE finds the IDs of all accounts and then deletes them, this is mainly to maintain the second-level cache, which is definitely not efficient. bulk delete/update is added in subsequent versions, but it cannot solve the cache maintenance problem. That is to say, HIBERNATE's batch operation efficiency is not satisfactory due to the maintenance of the second-level cache!
As we can see from the many points above, we often find a balance between efficiency and security/accuracy. In any case, optimization is not a pure technical problem, you should have a sufficient understanding of your applications and business characteristics. In general, the optimization scheme should be basically determined during the architecture design period. Otherwise, unnecessary rework may occur, resulting in project delays, as architects and project managers, we have to face possible complaints from developers. We have little control over changes to user requirements, however, technical/architecture risks should be realized and relevant countermeasures should be developed at the initial stage.
Note that the cache at the application layer is just a icing on the cake. Never take it as a life-saving tool, as well as the foundation of the application (database design, algorithms, efficient operation statements, and appropriate API selection) is the most important.
4. What about the cache mechanism of Hibernate?
1. The internal cache exists in Hibernate, which is also called a level-1 cache, which belongs to the application thing-level cache.
2. Secondary cache:
A) Application and Cache
B) distributed cache
Condition: the data is not modified by a third party, the data size is within the acceptable range, the data update frequency is low, the same data is frequently used by the system, non-critical data
C) third-party cache implementation
5. How to efficiently search for data within one month?
Avoid using sysdate for time comparison. for example: if you use select * from eventtable where eventdate> sysdate-30 to find, when the amount of data is small, it can not be seen, a large amount of data will find that the execution is slow, however, there are indexes on the date segment. Why is it slow? It turns out that Oracle constantly retrieves the ever-changing sysdate value while searching, instead of generating a conditional statement once we imagined and then searching. To speed up, We can first extract the date of the current day, convert it into a string, and then use the following statement for query. select * from eventtable where eventdate> to_date ('2017-12-1 ′, 'yyyy-mm-dd '). The speed varies by dozens of times.
6. What about Oracle operator optimization?
IN:
The SQL statements written IN are easy to write, clear, and easy to understand, but the performance is always relatively low. The steps executed in oracle are used to analyze the differences between SQL statements using IN and SQL statements without IN:
ORACLE tries to convert the IN statement to the join of multiple tables. If the conversion fails, the subquery in the in statement is executed first, and then the outer table record is queried. If the conversion is successful, you can directly query multiple tables by using the join method. Therefore, SQL statements using IN have at least one more conversion process. General SQL statements can be converted successfully, but SQL statements that contain grouping statistics cannot be converted.
Recommended method: Do not use the IN operator IN business-intensive SQL statements.
Not in:
It is NOT recommended because not in cannot apply table indexes.
Recommended Solution: use not exists or (Outer Join + is null)
Evaluate operator (not equal)
The operator that is not equal to the operator does not use indexes. The processing of the operator only generates a full table scan.
○ Recommended solution: replace it with other operation operations with the same function. For example, change a <> 0 to a> 0 or a <0 a <> ''To a>''
Is null or is not null (whether the field is null)
An index is generally not used to determine whether a field is null, because the B-tree index does not have a null index.
Recommended solution:
Replace it with other operation operations with the same function, for example, changing a is not null to a> 0 or a>.
Fields are not allowed to be empty, but a default value is used to replace null values. For example, status fields in the application for expansion cannot be empty. The default value is application.
Create a bitmap index (a partitioned table cannot be created, and it is difficult to control the bitmap index. If there are too many indexes in the field, the performance will be degraded, and the data block lock will be increased during multi-person update operations ).
And <operators (greater than or less than operators)
If the value is greater than or less than the operator, you do not need to adjust it because index search is used when it has an index. However, in some cases, you can optimize it. For example, a table has 1 million records, for A numeric field A, 0.3 million records A = 3. Therefore, the effect of executing A> 2 and A> = 3 is very different, because ORACLE will first find the record index of 2 and then compare it, when A> = 3, ORACLE directly finds the record Index = 3.
Ø LIKE:
The LIKE operator can be used for wildcard queries. The wildcard combinations in the LIKE operator may be almost arbitrary queries. However, poor use may result in performance problems, for example, LIKE '% 100' does not reference the index, while LIKE 'x5400%' references the range index. An actual example: the user ID following the Business ID in the YW_YHJBQK table can be used to query the Business ID YY_BH LIKE '% 100'. This condition will generate a full table scan, if you change to YY_BH LIKE 'x5400% 'OR YY_BH LIKE 'b5400%', the index of YY_BH will be used to query the two ranges, and the performance will be greatly improved.
7. What layers does the Oracle database architecture contain? What elements does each layer have?
The Oracle database includes a logical layer and a physical layer. The physical layer includes files on the Oracle disk. The logical layer is used to map data and physical layer files. The logical layer includes the following elements: one or more tablespaces. Database Schema: includes tables, clusters, indexes, views, stored procedures, database triggers, and sequences.
8. How to configure the applicationContext. xml file in the web environment?
<Listener>
<Listener-class>
Org. springframework. web. context. ContextLoaderListener
</Listener-class>
</Listener>
9. How to configure spring + struts?
Add a plug-in to the struts-config.xml, <plug-in className = "org. springframework. web. struts. ContextLoaderPlugIn">
<Set-property = "contextConfigLocation" value = "/WEB-INF/classes/applicationContext. xml"/>
</Plug-in>
Use it to load applicationContext. xml
Modify the action-mapping tag in the struts-config.xml, and the specific action is handed over to DelegateActionProxy Ø
Use DelegateActionProxy to enter the spring environment. U
Add <bean name = "/login" class = "" singleton = "false"/> to applicationContext. xml of spring.
10. What are the main classes in the spring + hibernate configuration file? How to configure it?
Add the spring environment to myeclipse and then the hibernate environment.
If spring and hibernate are combined, is the hibernate. cfg. xml file correct?
What are the main classes in the spring + hibernate configuration file? How to configure it?
DataSource
SessionFactory: hibernate. cfg. xml
TransactionManager
UserDao (extends HibernateDaoSupport)
SessionFactory
Facade
Proxy
SessionFactory
TransactionManager
Facade

The author "remembers the heart"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.