- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 maven、jpa、spring 和 hibernate 做一个项目。
TopicFacadeTest 类需要 20 分钟 o_O
插入和删除都非常慢。
CPU 在 1% 到 3% 之间。
我有 6G 内存。
有什么问题??这正常吗?
数据库最初是空的。
link Download Netbeans Project
这些是配置文件:
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="mycompanyPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>
test-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- holding properties for database connectivity /-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- enabling annotation driven configuration /-->
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:jpaVendorAdapter-ref="jpaAdapter">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="persistenceUnitName" value="mycompanyPU"></property>
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="${jpa.database}"
p:showSql="${jpa.showSql}"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.mycompany.repository" />
<context:component-scan base-package="com.mycompany.service" />
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.12:3306/mydatabase
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
jpa.database = MYSQL
hibernate.generate_statistics = true
hibernate.show_sql = true
jpa.showSql = true
jpa.generateDdl = true
Java 类:
主题.java
package com.mycompany.domain;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "topic")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = Topic.FIND_ALL, query = "SELECT t FROM Topic t"),
@NamedQuery(name = Topic.FIND_BY_ID, query = "SELECT t FROM Topic t WHERE t.id = :id"),
@NamedQuery(name = Topic.FIND_BY_NAME, query = "SELECT t FROM Topic t WHERE t.name = :name"),
@NamedQuery(name = Topic.FIND_BY_ID_CHAR, query = "SELECT t FROM Topic t WHERE t.idChar = :idChar")})
public class Topic implements Serializable {
private static final long serialVersionUID = 1L;
public static final String FIND_ALL = "Topic.findAll";
public static final String FIND_BY_ID = "Topic.findById";
public static final String FIND_BY_NAME = "Topic.findByName";
public static final String FIND_BY_ID_CHAR = "Topic.findByIdChar";
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "id", unique = true, insertable = false, updatable = false)
private Long id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "name")
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "idChar")
private String idChar;
public Topic() {
}
public Topic(String name, String idChar) {
this.name = name;
this.idChar = idChar;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdChar() {
return idChar;
}
public void setIdChar(String idChar) {
this.idChar = idChar;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Topic)) {
return false;
}
Topic other = (Topic) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.domain.Topic[ id=" + id + " ]";
}
}
AbstractFacade.java
package com.mycompany.repository;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.transaction.annotation.Transactional;
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
@Transactional
public void create(T entity) {
if (entity != null) {
getEntityManager().persist(entity);
}
}
@Transactional
public void edit(T entity) {
if (entity != null) {
getEntityManager().merge(entity);
}
}
@Transactional
public void remove(T entity) {
if (entity != null) {
getEntityManager().remove(getEntityManager().merge(entity));
}
}
@Transactional(readOnly = true)
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
@Transactional(readOnly = true)
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
@Transactional(readOnly = true)
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
@Transactional(readOnly = true)
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
主题外观
package com.mycompany.repository;
//<editor-fold defaultstate="collapsed" desc="imports">
import com.mycompany.domain.Topic;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
//</editor-fold>
@Stateless
@Repository
public class TopicFacade extends AbstractFacade<Topic> implements TopicFacadeLocal {
@PersistenceContext
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public TopicFacade() {
super(Topic.class);
}
@Transactional(readOnly = true)
@Override
public Topic findByName(String name) {
try {
Query query = em.createNamedQuery(Topic.FIND_BY_NAME);
query.setParameter("name", name);
return (Topic) query.getSingleResult();
} catch (NoResultException ex) {
return null;
}
}
@Transactional(readOnly = true)
@Override
public List<Topic> findByIdChar(String t) {
Query query = em.createNamedQuery(Topic.FIND_BY_ID_CHAR);
query.setParameter("idChar", t);
return query.getResultList();
}
}
TopicFacadeTest.java
package com.mycompany.repository;
//<editor-fold defaultstate="collapsed" desc="imports">
import com.mycompany.domain.Topic;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.embeddable.EJBContainer;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//</editor-fold>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class TopicFacadeTest {
//<editor-fold defaultstate="collapsed" desc="attributes">
@Autowired()
private TopicFacade topicFacade;
private Topic topic0;
private Topic topic1;
private Topic topic2;
private Topic topic3;
private Topic topic4;
private Topic topic5;
private Topic topic6;
private Topic topic7;
private Topic topic8;
private Topic topic9;
private List<Topic> topics;
//</editor-fold>
public TopicFacadeTest() {
}
//<editor-fold defaultstate="collapsed" desc="testCycle">
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
topic0 = new Topic("tema0", "t");
topic1 = new Topic("tema1", "t");
topic2 = new Topic("tema2", "t");
topic3 = new Topic("tema3", "t");
topic4 = new Topic("tema4", "t");
topic5 = new Topic("tema5", "t");
topic6 = new Topic("tema6", "t");
topic7 = new Topic("tema7", "t");
topic8 = new Topic("tema8", "d");
topic9 = new Topic("tema9", "k");
topicFacade.create(topic0);
topicFacade.create(topic1);
topicFacade.create(topic2);
topicFacade.create(topic3);
topicFacade.create(topic4);
topicFacade.create(topic5);
topicFacade.create(topic6);
topicFacade.create(topic7);
topicFacade.create(topic8);
topicFacade.create(topic9);
topics = new ArrayList();
topics.add(topic0);
topics.add(topic1);
topics.add(topic2);
topics.add(topic3);
topics.add(topic4);
topics.add(topic5);
topics.add(topic6);
topics.add(topic7);
topics.add(topic8);
topics.add(topic9);
}
@After
public void tearDown() {
topicFacade.remove(topic0);
topicFacade.remove(topic1);
topicFacade.remove(topic2);
topicFacade.remove(topic3);
topicFacade.remove(topic4);
topicFacade.remove(topic5);
topicFacade.remove(topic6);
topicFacade.remove(topic7);
topicFacade.remove(topic8);
topicFacade.remove(topic9);
topic0 = null;
topic1 = null;
topic2 = null;
topic3 = null;
topic4 = null;
topic5 = null;
topic6 = null;
topic7 = null;
topic8 = null;
topic9 = null;
topics = null;
}
//</editor-fold>
/**
* Test of create method, of class TopicFacade.
*/
@org.junit.Test
public void testCreate() throws Exception {
System.out.println("create");
Topic topic = new Topic();
topic.setName("tema99");
topic.setIdChar("t");
topicFacade.create(topic);
assertTrue(true);
topicFacade.remove(topic);
}
/**
* Test of edit method, of class TopicFacade.
*/
@org.junit.Test
public void testEdit() throws Exception {
System.out.println("edit");
topic1.setIdChar("vf");
topicFacade.edit(topic1);
assertTrue(true);
assertEquals("vf", topicFacade.find(topic1.getId()).getIdChar());
}
/**
* Test of remove method, of class TopicFacade.
*/
@org.junit.Test
public void testRemove() throws Exception {
System.out.println("remove");
topicFacade.remove(topic0);
assertEquals(null, topicFacade.find(topic0.getId()));
}
/**
* Test of find method, of class TopicFacade.
*/
@org.junit.Test
public void testFind() throws Exception {
System.out.println("find");
assertEquals(topic0, topicFacade.find(topic0.getId()));
assertNotSame(topic0, topicFacade.find(topic1.getId()));
assertEquals(null, topicFacade.find(null));
assertEquals(null, topicFacade.find(-747474));
}
/**
* Test of findAll method, of class TopicFacade.
*/
@org.junit.Test
public void testFindAll() throws Exception {
System.out.println("findAll");
List<Topic> topicsFindAll = topicFacade.findAll();
assertNotNull(topicsFindAll);
assertEquals(10, topicsFindAll.size());
}
/**
* Test of findRange method, of class TopicFacade.
*/
@org.junit.Test
public void testFindRange() throws Exception {
System.out.println("findRange");
int[] range = {4, 10};
List<Topic> topics = topicFacade.findRange(range);
for (Topic topic : topics) {
System.out.println(topic);
}
}
/**
* Test of count method, of class TopicFacade.
*/
@org.junit.Test
public void testCount() throws Exception {
System.out.println("count");
assertEquals(10, topicFacade.count());
tearDown();
assertEquals(0, topicFacade.count());
}
/**
* Test of findByName method, of class TopicFacade.
*/
@Test
public void testFindByName() throws Exception {
System.out.println("findByName");
assertEquals(topic0, topicFacade.findByName("tema0"));
assertNotSame(topic0, topicFacade.findByName("tema1"));
assertNotSame(null, topicFacade.findByName("tema0"));
assertEquals(null, topicFacade.findByName("jafasdg"));
}
/**
* Test of findByIdChar method, of class TopicFacade.
*/
@Test
public void testFindByIdChar() throws Exception {
System.out.println("findByIdChar");
List<Topic> topicsByIdChar = topicFacade.findByIdChar("t");
assertEquals(8, topicsByIdChar.size());
for (Topic topic : topicsByIdChar) {
System.out.println(String.format("%s-%s", topic.getId(), topic.getIdChar()));
}
tearDown();
topicsByIdChar = null;
topicsByIdChar = topicFacade.findByIdChar("t");
assertTrue(true);
}
}
已编辑:可能的解决方案 |数据源(连接池),添加依赖
test-context.xml
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
pom.xml
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.39</version>
</dependency>
最佳答案
首先,将 DriverManagerDataSource
替换为实际汇集连接的数据源。这个数据源只是委托(delegate)给驱动程序管理器来打开\关闭每个事务的连接
关于spring - maven + jpa + hibernate + Spring 。太慢了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16199876/
我用 cudaMemcpy()一次将 1GB 的数据精确复制到设备。这需要 5.9 秒。反之则需要 5.1 秒。这是正常的吗?函数本身在复制之前有这么多开销吗? 理论上,PCIe 总线的吞吐量至少应为
我正在尝试读取图像的大小并在其高度大于 150 时调整其边距。但是在运行这段代码时,我总是在控制台中得到一个“0”: var coverImg; coverImg =
我正在开发一个 iPhone 应用程序,其中包含一些标准的“相机”功能。保存到相机胶卷真的太慢了,在iPhone 4上大约需要四秒钟。有什么办法可以提高速度吗? 如果您查看默认的 iPhone 相
我创建了一个将图像转换为灰度的类。但它的工作速度太慢了。有没有办法让它运行得更快? 这是我的类(class): @implementation PixelProcessing SYNTHESIZE_S
我使用以下代码,结果是正确的,但 gethostbyaddr 需要大约 30 秒。 function IPAddrToName(IPAddr: string): string; var So
我有以下案例, public class Test { private static final int MAX_NUMBER = 10_00_00; public static vo
我已经正确添加了所有必需的 JARS: Ucanaccess 3.0.4 commons-lang-2.6 commons-logging-1.1.1 hsqldbd jackcess-2.1.3 我
我为特定功能构建了一个多处理密码破解程序(使用单词列表),与使用单个进程相比,它减少了一半的时间。 最初的问题是,它会向您显示破解的密码并终止工作人员,但剩余的工作人员将继续工作,直到他们用完可哈希的
我在我的一个 JSP 中引入了 Sencha 网格。本地 sencha 相当快,但在外部服务器上它太慢了。 我在这里按照部署说明进行操作 http://docs.sencha.com/ext-js/4
我的查询加载时间有很大问题。在这种情况下,我需要 hg_ft_won 列(表:值)中的值,用于 home_team_id 和 away_team_id(表:匹配)。 它确实可以正常工作。加载只需要很长
我现在正在学习不同类型的排序,我发现,从某个点开始,我的快速排序算法根本无法快速工作。 这是我的代码: class QuickSort { // partitioning arr
为什么要模式 [0123]123456|98765 比在 Java 中执行 [0123]123456 然后 98765 慢两倍?所以单独搜索它们比用 OR 执行更快。有人有解释吗? UPD 查看带有结
我有带 Assets 的 Android 应用程序。它们包含 20,000 多个文件,其中大部分是简单的文本或 png 文件,分为不同的文件夹和子文件夹。1 个单个文件的最大大小为 500kb,其中
您好,我在查询中添加了 GROUP_CONCAT 函数,该函数终止了我的查询:/。我的查询是: SELECT u.username,a.user_id,a.id,a.text,a.lang as fr
我正在寻找优化查询的想法。 目前,我有一个 4M 行的表,我只想检索引用的最后 1000 行: SELECT * FROM customers_material_events WHERE refere
我在我的应用程序中使用 NSURLConnection,我在其中扫描条形码,通过 NSURLConnection 发送 XML,Java 服务向我发回 XML。我的问题是,使用 Wifi 时,响应时间
当我运行以下程序时,执行大约需要 7 到 8 分钟。我真的不确定我哪里弄错了,因为这个程序执行起来要花很多时间。 public class Test { public stat
我正在使用 NSFetchResultsController 从数据库中接收项目(有 80.000 个项目)。 这是我的谓词:@"(desc CONTAINS[cd] %@)", [any text]
我在 x_data 中有一个 3x2000 numpy 数组,在 y_data 中有一个 1x2000 numpy 数组,我将其传递给此函数 regress 以给我一条回归线。它工作正常。问题是我正在
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。所以我一开始的代码是 for (int y = 0; y (y, x); // read pixel (0,0)
我是一名优秀的程序员,十分优秀!