我在与Maria DB相同的主机上有一个spring-boot应用程序,两者都运行正常一段时间.但是在12小时到2天之间,Spring启动应用程序似乎失去了与数据库的连接(stacktrace),并且无法从中恢复.
当我重新启动弹簧应用程序时,一段时间再次正常.
应用程序没有负载,当它失去连接时,应用程序仍在工作,但数据库连接无法恢复.数据库在此期间没有重启(正常运行时间为4周).只有监控服务对每分钟ping一次DB的应用程序进行ping操作.(春季靴子健康)
连接到同一个数据库的其他Java应用程序运行正常,没有任何问题.
我的问题是:
为什么spring无法从该错误中恢复并尝试重新连接到DB?如何设置弹簧重新连接到DB?
2015-02-19 15:25:48.392 INFO 4931 [qtp92662861-19] --- o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2015-02-19 15:25:48.580 INFO 4931 [qtp92662861-19] --- o.s.jdbc.support.SQLErrorCodesFactory : SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
2015-02-19 15:25:48.616 WARN 4931 [qtp92662861-19] --- o.s.jdbc.support.SQLErrorCodesFactory : Error while extracting database product name - falling back to empty error codes
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed. …Run Code Online (Sandbox Code Playgroud) 我已经使用Spring Boot 1.0.2.RELEASE成功开发了一个原型(直到今天为1.0.1.RELEASE).
我搜索并搜索过如下解决方案: Spring Boot jdbc datasource autoconfiguration在独立的tomcat上失败 Spring Boot/Spring数据import.sql不运行Spring-Boot-1.0.0.RC1
他们都建议让Spring Boot完成这项工作.当使用H2时,一切正常,但当我尝试切换到PostgreSQL时,我得到:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(org.springframework.orm.jpa.JpaVendorAdapter)] threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined
Run Code Online (Sandbox Code Playgroud)
我的build.gradle如下:
loadConfiguration()
def loadConfiguration() {
def environment = hasProperty('env') ? env : 'dev'
project.ext.envrionment = environment
println "Environment is set to $environment"
def configFile = …Run Code Online (Sandbox Code Playgroud) 我正在研究Vaadin spring应用程序.我唯一能说的是,用户的身份验证/授权必须通过查询数据库来完成jdbcTemplate.如何解决这个问题?我正在使用Spring Boot 1.4.2.RELEASE.
Description:
The dependencies of some of the beans in the application context form a cycle:
???????
| jdbcAccountRepository defined in file [repositories\JdbcAccountRepository.class]
? ?
| securityConfiguration.WebSecurityConfig (field services.JdbcUserDetailsServicessecurity.SecurityConfiguration$WebSecurityConfig.userDetailsService)
? ?
| jdbcUserDetailsServices (field repositories.JdbcAccountRepository services.JdbcUserDetailsServices.repository)
???????
Run Code Online (Sandbox Code Playgroud)
原始代码如下所示:
AccountRepository:
public interface AccountRepository {
void createAccount(Account user) throws UsernameAlreadyInUseException;
Account findAccountByUsername(String username);
}
Run Code Online (Sandbox Code Playgroud)
JdbcAccountRepository:
@Repository
public class JdbcAccountRepository implements AccountRepository {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
private final JdbcTemplate jdbcTemplate;
private final PasswordEncoder passwordEncoder;
@Autowired …Run Code Online (Sandbox Code Playgroud) 目前我通过向数据源bean id添加属性来在spring中将autocommit设置为false,如下所示:
<property name="defaultAutoCommit" value="false" />
Run Code Online (Sandbox Code Playgroud)
但是我需要在执行我的程序之前在一个java方法中专门添加它.我使用了下面的代码片段.
getJdbcTemplate().getDataSource().getConnection().setAutoCommit(false);
Run Code Online (Sandbox Code Playgroud)
但上面的一行并没有将autocommit设置为false?
我错过了什么吗?
或者通过spring在特定java方法中设置autocommit的任何替代方法
谢谢
尝试使用Spring-JDBC.我用这个作为参考.我正在尝试获得具有相同姓氏的演员列表.运行此代码给了我想要的结果:
public List<String> getActorsWithSameLastName(String lastName,
NamedParameterJdbcTemplate template) {
String query = "SELECT FIRSTNAME FROM ACTORS WHERE LASTNAME=:LASTNAME";
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("LASTNAME", lastName);
return template.queryForList(query, paramMap, String.class);
}
Run Code Online (Sandbox Code Playgroud)
我有一个List<String>姓氏.如何获得具有列表的演员列表?我是否遍历姓氏列表并且getActorsWithSameLastName()每次调用或者弹簧是否提供了迭代的方式并为我提取结果?请指教.
从Spring JDBC文档中,我知道如何使用JdbcTemplate插入blob
final File blobIn = new File("spring2004.jpg");
final InputStream blobIs = new FileInputStream(blobIn);
jdbcTemplate.execute(
"INSERT INTO lob_table (id, a_blob) VALUES (?, ?)",
new AbstractLobCreatingPreparedStatementCallback(lobhandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator)
throws SQLException {
ps.setLong(1, 1L);
lobCreator.setBlobAsBinaryStream(ps, 2, blobIs, (int)blobIn.length());
}
}
);
blobIs.close();
Run Code Online (Sandbox Code Playgroud)
以及如何检索新插入行的生成密钥:
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(INSERT_SQL, new String[] {"id"});
ps.setString(1, name);
return ps;
}
},
keyHolder);
// …Run Code Online (Sandbox Code Playgroud) 我想将我的应用程序中的一些大型查询外部化到properties\sql\xml文件中.然而,我想知道是否有人有一些关于如何以干净的方式实现这一点的建议.大多数结果建议使用ORM框架,但由于某些数据限制,这不适用.
我看了一下:Java - 在一个外部文件中存储SQL语句,但为一些查询执行这个属性名.1,.2等,每个查询都要长20行看起来不干净.
我正在尝试通过自动装配数据库
@Autowired
private DataSource dataSource;
Run Code Online (Sandbox Code Playgroud)
我有一个数据源 application.yml
spring:
profiles:
active: dev
---
spring:
profiles: dev
datasource:
driverClassName: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/dbname
username: user
password: password
name: dev
logging:
level:
org.springframework: INFO
---
spring:
profiles: prod
name: prod
logging:
level:
org.springframework: INFO
Run Code Online (Sandbox Code Playgroud)
但是我收到一条错误消息.
Could not autowire. There is more than one bean of 'DataSource' type.
Beans:dataSource (DataSourceConfiguration.class)
dataSource (DataSourceConfiguration.class)
Run Code Online (Sandbox Code Playgroud)
我发现这很奇怪,因为我只有一个数据源定义在application.yml我的知识中,据我所知,我没有定义任何其他数据源.
我尝试了配置,但我仍然遇到同样的问题.
@Configuration
@EnableConfigurationProperties
public class AppConfig {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的'pom'文件
<?xml version="1.0" …Run Code Online (Sandbox Code Playgroud) 我有一个带有Foos的MySQL表.每个Foo都有一个数字非唯一代码和一个名称.现在我需要查找是否有任何具有某些代码的Foo恰好具有以给定字符串开头的名称.在普通的SQL中,这将是微不足道的:
select * from FOO where CODE in (2,3,5) and NAME like 'bar%';
Run Code Online (Sandbox Code Playgroud)
但是我现在如何在Spring中正确地做到这一点?如果不需要'like'运算符,我会这样做:
public List<Foo> getByName(List<Integer> codes, String namePart) {
String sql = "select * from FOO where CODE in (:codes) and NAME=:name"
Map<String,Object> params = new HashMap<String,Object>();
params.put("codes", codes);
params.put("name", namePart);
return getSimpleJdbcTemplate().query(sql, new FooRowMapper(), params);
}
Run Code Online (Sandbox Code Playgroud)
然而,与"喜欢"似乎没有任何工作:NAME like :name%,NAME like ':name%'或NAME like ?%使用占位符代替命名参数时.
我可能是残酷的并且输入它
String sql = "select * from FOO where CODE in (:codes) and NAME like '"+namePart+"%'";`
Run Code Online (Sandbox Code Playgroud)
但是很明显,如果Spring能够正确地清理输入参数等,那就太好了,你知道......
你会认为Spring会以某种方式支持它,但我无法弄明白.
我在我的Web应用程序中使用spring-boot并使用spring-jpa从/向我的数据库读/写.它工作得很好,但我想了解如何管理数据库连接.以下是我的数据库属性配置:
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=user
spring.datasource.password=pwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-active=500
Run Code Online (Sandbox Code Playgroud)
我已将最大连接数设置为500.当用户在我的spring应用程序上发出请求时,将为他打开数据库连接.完成请求后,春天jpa会关闭这个连接吗?如果没有,它何时关闭未使用的连接?
我已经阅读了http://docs.spring.io/spring-data/jpa/docs/current/reference/html/上的spring jpa参考文档.但它没有提到关于连接的任何事情.
spring-jdbc ×10
spring ×7
java ×6
spring-boot ×3
jdbctemplate ×2
hibernate ×1
mariadb ×1
postgresql ×1
spring-mvc ×1
sql ×1
vaadin ×1